Javascript 如何在开发者控制台中暂停脚本执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8396563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pause script execution in Developers console
提问by ptamzz
I'm trying to create a rotating banner (JavaScript & CSS). For debugging and making changes on the CSS etc in the developers' console, I want to pause the JavaScript execution while I make changes and then run it or so on. So, is there a way to pause the script execution in the browser?
我正在尝试创建一个旋转横幅(JavaScript 和 CSS)。为了在开发人员的控制台中调试和更改 CSS 等,我想在进行更改时暂停 JavaScript 执行,然后运行它等等。那么,有没有办法在浏览器中暂停脚本执行呢?
采纳答案by Jpsy
You are looking for "breakpoints".
您正在寻找“断点”。
Which browser are you using?
您使用的是哪个浏览器?
Chrome supports breakpoints right away in its developer tools:F12 (or Ctrl-Shift-I), "Script" tab, select script from dropdown, click the line number.
Update:
On PC: F12or Ctrl+Shift+I/ On Mac: Cmd+Alt+I
select "Sources" tab, select script from the file pane on the left, click the line number.
Chrome 在其开发人员工具中立即支持断点:F12(或 Ctrl-Shift-I),“脚本”选项卡,从下拉列表中选择脚本,单击行号。
更新:
在PC:F12或Ctrl+ Shift+ I/在Mac:Cmd+ Alt+I
选择“源”选项卡中,从左侧的文件窗格中选择脚本,单击行号。
In Firefox use the Firebug extension:
On PC and Mac: F12,
"Script" tab, activate & reload if needed, select script from dropdown, click line number.
在Firefox中使用Firebug扩展:
在PC和Mac: F12,
“脚本”选项卡,激活和重载如果从下拉菜单需要,选择脚本,单击行号。
When your Javascript pauses at a breakpoint, both browsers offer you the usual debugging tools to single step through the code, inspect & change variable values, watch expressions,...
当您的 Javascript 在断点处暂停时,两种浏览器都会为您提供常用的调试工具来单步调试代码、检查和更改变量值、监视表达式、...
回答by Kenny Evitt
As katspaughmentions in their comment:
F8
F8
This only works for me from the Sourcestab of the Developer Toolswindow in Chrome 59.0.3071.115 (on Mac OS X).
这仅适用于 Chrome 59.0.3071.115(在 Mac OS X 上)中开发者工具窗口的源选项卡。
回答by Zihao Zhao
You can write a pause code yourself. Pause javascript excution using debugger. In the chrome console, run:
您可以自己编写暂停代码。使用调试器暂停 javascript 执行。在 Chrome 控制台中,运行:
window.addEventListener('keydown', function(event) {
if (event.defaultPrevented) {
return; // Should do nothing if the default action has been cancelled
}
let handled = false;
if (event.keyCode == 123) {
handled = true;
debugger; // 123 is the keyCode of F12
}
if (handled) {
// Suppress "double action" if event handled
event.preventDefault();
}
});
Highlight element with inspector
使用检查器突出显示元素
Hit F12
按F12