2011-04-15 114 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