您可以使用以下内容“冻结”页面本身:
var start = new Date().getTime(), delay = 5000; // milliseconds
while(new Date().getTime() < start+delay) {}
However this will freeze the entire browser too and usually results in a crashed browser, a "Not Responding" box from Windows, or a "this script is taking a long time to run, kill it?" question.
The "proper" way to do it would be to have a "isPaused" boolean that you can flip on or off, and if it's off then functions try to run themselves again. Something like this:
function pauseCheck(thisobj,argobj) {
if( isPaused) {
setTimeout(function() {argobj.callee.apply(thisobj,Array.prototype.slice(argobj,0));},250);
return false;
}
return true;
}
Then in your other functions:
function doSomething(x,y) {
if( pauseCheck(this,arguments)) {
// function body here
}
}