window.location affected by escape key?
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.
精彩评论