Posted By

pedromagalhaes on 04/19/11


Tagged

setTimeout


Versions (?)

Execute something if over something after X seconds


 / Published in: jQuery
 

I created this snippet for a personal project, where i needed to open a tooltip only when the user was over or moving the mouse after X seconds in a specific selector. In the code you will find "your code goes here" where you can play around like showing or hiding divs, or whatever are your needs.

  1. $(this).mouseover(function(){
  2.  
  3. $seconds = 0;
  4. $timer = 0;
  5. count();
  6.  
  7. function count(){
  8. $seconds = setTimeout(function() {
  9. count();
  10. }, 1000);
  11. $timer = $timer + 1;
  12. if($timer == 2){
  13. // your code goes here...
  14. clearTimeout($seconds);
  15. }
  16. }
  17.  
  18. }).mousemove(function(kmouse){
  19. if($timer == 2){
  20. // your code goes here...
  21. clearTimeout($seconds);
  22. }
  23. }).mouseout(function(){
  24. clearTimeout($seconds);
  25. // your code goes here...
  26. });

Report this snippet  

You need to login to post a comment.