A solution that does not support selectors for jquery in setTimeout

  • 2020-06-03 05:46:50
  • OfStack

Today, when I was writing an js delay event, I found that using jquery's $(this) in the setTimeout method did not work. After various tests, I finally concluded that jquery's selector was not supported in setTimeout. So consult 1 QQ do jquery development master, immediately solved the problem, here 1 record.
Here is the js code when the author did the delay processing:


$('.dl_select dt').hover( 
  function(){ 
    clearTimeout(t3); 
    $(this).siblings('dd').css({'display':'block','cursor':'pointer'}); 
  }, 
  function(){ 
    t2=setTimeout(function(){$(this).siblings('dd').css({'display':'none'});},300); 
  } 
); 
$('.dl_select dd').hover( 
  function(){ 
    clearTimeout(t2); 
    $(this).css({'display':'block','cursor':'pointer'}); 
  }, 
  function(){ 
    t3=setTimeout(function(){$(this).css({'display':'none'});},200); 
  } 
); 

Note the code in setTimeout in the above code. If the code is not in this method, it is fine, but the above case will report an error. As for the reason, the author does not now understand. After the netizen point change as follows have no matter, the method is very clever. Here is the correct code:


$('.dl_select dt').hover( 
  function(){ 
    clearTimeout(t3); 
    $(this).siblings('dd').css({'display':'block','cursor':'pointer'}); 
  }, 
  function(){ 
    var $this=$(this).siblings('dd'); 
    t2=setTimeout(function(){$this.css({'display':'none'});},300); 
  } 
); 
$('.dl_select dd').hover( 
  function(){ 
    clearTimeout(t2); 
    $(this).css({'display':'block','cursor':'pointer'}); 
  }, 
  function(){ 
    var $this=$(this); 
    t3=setTimeout(function(){$this.css({'display':'none'});},200); 
  } 
); 

This is the end of this article, I hope you enjoy it.


Related articles: