window.location受转义键影响?

时间:2011-04-15 19:10:30

标签: javascript

我有一个计时器来刷新页面,它正在运行:

updateCountdown = function() {
    if (Variables.Seconds == Infinity) {
    } else if (Variables.Seconds == 0) {
        $countdown.text('Bam!');
        window.location.replace(Variables.PageName);
    } else {
        $countdown.text(Variables.Seconds--);
        setTimeout(updateCountdown, 1000);
    }
};

然后我有了这个:

document.onkeydown=function(e) {

    if (e.which==27) {
        Variables.Seconds = 0;
        updateCountdown();
    }
};

当我按下escape时,$ countdown.text会显示'Bam!',但是当Variables.Seconds通常递减为0时,页面不会像它一样刷新。

1 个答案:

答案 0 :(得分:4)

调用updateCountdown()后,您可能需要执行return false;;取消Escape的默认操作。

   document.onkeydown=function(e) {

        if (e.which==27) {
            Variables.Seconds = 0;
            updateCountdown();
            return false;
        }
    };

如果您正在使用其他JavaScript库this post可能会有所帮助。