admin管理员组

文章数量:1333450

I'm writing a script for the serial extraction of information from a page, in general I have to pause javascript, but that the page continues to load, and the javascript stopped.

setTimeout is not necessary, since the rest of the script is still running

I need it in order to make the script continues to run after the other script (which I do not have access) download the necessary data to the page (this takes 3 seconds).

P.S. If there is something I pull information from the village - / using mozilla with extention "user script"

I'm writing a script for the serial extraction of information from a page, in general I have to pause javascript, but that the page continues to load, and the javascript stopped.

setTimeout is not necessary, since the rest of the script is still running

I need it in order to make the script continues to run after the other script (which I do not have access) download the necessary data to the page (this takes 3 seconds).

P.S. If there is something I pull information from the village - http://www.mosgortrans/pass3/ using mozilla with extention "user script"

Share Improve this question asked Dec 1, 2012 at 10:28 Ilya KharlamovIlya Kharlamov 1251 gold badge1 silver badge10 bronze badges 1
  • 3 rather than pausing the script, I would fire an event and write my logic in that event handler code in the js. This way it is not dependent on number of seconds i.e 3 or 5 or anything. – Mandar Commented Dec 1, 2012 at 10:45
Add a ment  | 

2 Answers 2

Reset to default 2

I don't see why setTimeout is not necessary here - it does exactly what you describe:

setTimeout(function() {
    // this code runs 3 seconds after the page loads
}, 3000);

As the previous answer and ment have suggested, the normal way of doing this would be to put the code you want to run after the script loads in a function in setTimeout. If you are worried, for instance, about event handlers being triggered while you are waiting and causing an error, then you need to disable the event handlers (e.g. element.onclick = null) then re-enable them within the time-out function. I suppose you could also do something like this:

var pause = false;
...
callExternalScript();
pause = true;    
setTimeout(function() {
   pause = false;
}, 3000);
...
function oneOfMyOtherFunctions() {
    if (pause) return;
    ...
}
...

but this is messy because you have to include if (pause) return at the start of every function that you want to disable while the script is paused. Also you may or may not want to add extra code to run all those functions that were called while the script was paused, once it has been un-paused.

本文标签: htmlsleepwaitpause javascriptStack Overflow