2011-10-24 12 views
1

私はマウスがオブジェクトの中にn秒以上を費やした場合にのみトリガーしたいonmouseenterイベントを持っています。jQueryタイマー機能

$('#object').live('mouseenter', function() { 

// code that will execute only if mouse spends more than n seconds 

}); 

私は何か助けていただきありがとうございます。 「作業」テキストを変更し、赤に色を変更する例えば

ので
+0

[フォーカスチェック用](http://stackoverflow.com/quest ion/967096/use-j-if-in-the-input-to-focus)を使用している。 [タイマー用](http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm) – Vap0r

答えて

0

あなたは、遅延の後に呼び出される関数を設定することができますmouseleaveと表示された場合はキャンセルしてください:

(function() { 
    var timer = 0; // 0 is a safe "no timer" value 

    $('#object').live('mouseenter', function() { 
     // I'm assuming you don't want to stomp on an existing timer 
     if (!timer) { 
      timer = setTimeout(doSomething, 1000); // Or whatever value you want 
     } 
    }).live('mouseleave', function() { 
     // Cancel the timer if it hasn't already fired 
     if (timer) { 
      clearTimeout(timer); 
      timer = 0; 
     } 
    }) 

    function doSomething() { 
     // Clear this as flag there's no timer outstanding 
     timer = 0; 

     // Do your stuff 
    } 

})(); 
+0

ありがとう。それは本当に助けになった – suenda

4
var timeout; 
$('#object').live('mouseenter', function() { 
    timeout = setTimeout(function(){ 
     // code that will execute only if mouse spends more than n seconds 
    }, 2000); 

}); 

$('#object').live('mouseleave', function() { 
    clearTimeout(timeout); 
}); 

、コードを以下のことは結構です

<div id="object">Hover here and wait</div> 
<script> 
var timeout; 
var object = $("#object"); 
object.live('mouseenter', function() { 
    timeout = setTimeout(function(){ 

     object.css({'color': 'red', 'font-size': '400%'}); 
     object.html('Working'); 
    }, 2000); 
}); 

object.live('mouseleave', function() { 
    clearTimeout(timeout); 
}); 
</script> 

demo

+0

助けてくれてありがとう。できます。ありがとう。 – suenda

関連する問題