If you are willing to accept an interference for the user, you could use the debugger statement , as it is available in all major browsers.
(如果您愿意接受用户的干扰,则可以使用debugger语句 ,因为该语句在所有主流浏览器中都可用。)
Side note: If the users of your app are interested in console usage, they're probably familiar with dev tools, and will not be surprised by it showing up.
(旁注:如果您的应用程序用户对控制台的使用感兴趣,那么他们可能熟悉开发工具,并且不会对它的出现感到惊讶。)
In short, the statement is acting as a breakpoint, and will affect the UI only if the browser's development tools is on .
(简而言之,该语句充当断点,并且仅在打开浏览器的开发工具时才会影响UI。)
Here's an example test:
(这是一个示例测试:)
<body>
<p>Devtools is <span id='test'>off</span></p>
<script>
var minimalUserResponseInMiliseconds = 100;
var before = new Date().getTime();
debugger;
var after = new Date().getTime();
if (after - before > minimalUserResponseInMiliseconds) { // user had to resume the script manually via opened dev tools
document.getElementById('test').innerHTML = 'on';
}
</script>
</body>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…