2011-04-15 115 views
0

我有一个计时器刷新页面,它的工作:受escape key影响的window.location?

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

当我按下逃生,然后$ countdown.text说,但页面不像每当Variables.Seconds正常递减为0时那样刷新。

回答

4

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

document.onkeydown=function(e) { 

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

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

+1

e.preventDefault()!我在想什么! – 2011-04-15 19:38:39

+0

@cf_PhillipSenn我已经学会了这种艰难的方式(浪费了类似的问题)。 – LoveGandhi 2011-04-15 19:42:14