最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google chrome - Pause Javascript when executing it in browser console - Stack Overflow

programmeradmin3浏览0评论

I need to execute a Javascript in the console of Chrome with some delay. How can this be done?

The reason I need this is that as soon the focus changes from the webpage (or a certain element of the webpage) it is redrawn. Therefore I want to start the script I execute in the console with a few seconds delay so I can change the focus to the right element on the page I am working with.

Edit 1: Dinesh Padiyan.

Doesn't work for me, se screen shot:

I need to execute a Javascript in the console of Chrome with some delay. How can this be done?

The reason I need this is that as soon the focus changes from the webpage (or a certain element of the webpage) it is redrawn. Therefore I want to start the script I execute in the console with a few seconds delay so I can change the focus to the right element on the page I am working with.

Edit 1: Dinesh Padiyan.

Doesn't work for me, se screen shot:

Share Improve this question edited Nov 20, 2018 at 15:30 asked Nov 20, 2018 at 14:44 user10409695user10409695 5
  • 1 You better use a debugger to walk through the code. – VLAZ Commented Nov 20, 2018 at 14:49
  • Have you tried putting debugger; statement at the point you want to enforce the execution halt? See more info here: w3schools./jsreF/jsref_debugger.asp – vahdet Commented Nov 20, 2018 at 14:51
  • I think that debugger; will stop all javascript from being executed. – Michal Commented Nov 20, 2018 at 15:01
  • You need to use double quotes . I have edited my answer accordingly. – Dinesh Pandiyan Commented Nov 20, 2018 at 15:33
  • 1 The line should be console.log("I won't execute until you say so"); – Dinesh Pandiyan Commented Nov 20, 2018 at 15:37
Add a ment  | 

3 Answers 3

Reset to default 8

If you need to delay for debugging purposes, you will need to add debugger; statements to your code. When you add debugger and have your developer tools open, script execution will pause at your debugger; statement and you can inspect your code in the developer tools.

console.log('I will execute');
debugger; // execution will pause at this line
console.log("I won't execute until you say so");

If you need to delay execution for your site behavior, you can use setTimeout() to delay invocation.

console.log('I will be printed immediately');
setTimeout(function() {
  console.log('I will be printed after 5s');
}, 5000); // 5s delay

You can extend console object like this and then use custom logLater method.

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);

function delay (ms){   return new Promise(resolve => setTimeout(resolve, s));  }

"async" working demo on: http://zarsoft.info/software/_Test/pause_javascript/index.html

发布评论

评论列表(0)

  1. 暂无评论