1

所以我有一个递归调用自己的计时器:

function introAnimation()
{
    var ostream = document.getElementById('table');

    var newhtml = '<h1>Ready?</h1><br/>';
    newhtml += '<h1>' + countdown-- + '</h1>';

    ostream.innerHTML = newhtml;

    if (countdown > 0)
        var a = setTimeout(function() {introAnimation()}, 1000);

}

但问题是程序在完成计时器之前继续运行。有没有办法让所有其他进程停止运行,直到指定的函数停止?

4

1 回答 1

0

有没有办法让所有其他进程停止运行,直到指定的函数停止?

是和不是。方法是无限循环(while(true) ;),但这是不希望的,因为它会冻结您的浏览器并且永远不会停止(因为超时无法拦截正在运行的函数)。所以你不应该。你真正想问的是:

如何在超时后推迟我的程序继续?

使用回调。你已经在你的 中使用了introAnimation它,所以应该不难。将该功能更改为

function introAnimation(callback) {
    var ostream = document.getElementById('table');

    var newhtml = '<h1>Ready?</h1><br/>';
    newhtml += '<h1>' + countdown-- + '</h1>';

    ostream.innerHTML = newhtml;

    if (countdown > 0)
        var a = setTimeout(function() {introAnimation(callback)}, 1000);
    else
        callback();
}

和你的程序

introAnimation();
// rest of program

introAnimation(function() {
    // part of the program that should run when the itro is done
});
// part of the programm that runs during the intro animation
// (might be nothing)
于 2013-06-10T03:57:23.033 回答