开发者

window.location affected by escape key?

开发者 https://www.devze.com 2023-02-25 16:35 出处:网络
I have a timer to开发者_Python百科 refresh the page, and it\'s working: updateCountdown = function() {

I have a timer to开发者_Python百科 refresh the page, and it's working:

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

Then I have this:

document.onkeydown=function(e) {

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

When I press escape, then $countdown.text says 'Bam!', but the page does not refresh like it does whenever Variables.Seconds normally decrements to 0.


You might need to do return false; after you call updateCountdown(); to cancel the default action of Escape.

   document.onkeydown=function(e) {

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

If you are using other javascript libraries this post might be of help.

0

精彩评论

暂无评论...
验证码 换一张
取 消