2012-10-05 50 views

回答

2

首先,歡迎來到Stack Overflow。通常習慣於提供一些你已經嘗試過的例子。下次嘗試一下。當我們看到一些代碼時,我們會更有幫助!

這應該讓你開始。它將簡單地console.logenter/return鍵被按下。一旦它被釋放,它將再次產生console.log,並在下一次按下enter/return鍵時重新綁定事件。

Here is an example fiddle

(function(){ 
    var intervalId; 

    // your animations should go here 
    var someFunc = function(){ 
    console.log("executing"); 
    } 

    var keyDownFn = function(e){ 
    if(e.keyCode == 13){ // 13 is enter/return 
     $(window).unbind('keydown.animation'); 
     $(window).bind('keyup.animation', keyUpFn); 
     intervalId = setInterval(someFunc, 500); 
    } 
    } 

    var keyUpFn = function(e){ 
    if(e.keyCode == 13){ // 13 is enter/return 
     console.log("stopping"); 
     $(window).unbind('keyup.animation'); 
     $(window).bind('keydown.animation', keyDownFn); 
     clearInterval(intervalId); 
    } 
    } 

    // this line fires the whole thing off 
    $(window).bind('keydown.animation', keyDownFn); 

})();​