我需要在Chrome的控制台上执行Javascript,并有一些延迟。这是如何做到的呢?
我需要这样做的原因是,一旦焦点从网页(或网页的某个元素)改变,它就会被重新绘制。因此,我希望在控制台中启动脚本,延迟几秒钟,这样我就可以将焦点更改为正在处理的页面上的正确元素。
编辑1: Dinesh Padiyan。
不适合我,屏幕截图:

发布于 2020-03-29 17:32:53
function delay (ms){ return new Promise(resolve => setTimeout(resolve, s)); }“异步”工作演示:javascript/index.html
发布于 2018-11-20 14:50:30
如果需要延迟调试,则需要将debugger;语句添加到代码中。添加调试器并打开开发工具时,脚本执行将在debugger;语句处暂停,您可以在开发工具中检查代码。
console.log('I will execute');
debugger; // execution will pause at this line
console.log("I won't execute until you say so");如果需要延迟站点行为的执行,可以使用setTimeout()延迟调用。
console.log('I will be printed immediately');
setTimeout(function() {
console.log('I will be printed after 5s');
}, 5000); // 5s delay
发布于 2018-11-20 14:55:47
您可以像这样扩展console对象,然后使用自定义logLater方法。

console.logLater = (message, delay) => setTimeout(function() {
console.log(message);
}, delay);
console.log('I will be printed immediately');
console.logLater('I will be printed after 3 seconds', 3000);
https://stackoverflow.com/questions/53395496
复制相似问题