문제

I am creating I am creating a drop down which I want to delay about 250 ms so that it's not triggered when someone quickly scrolls across the button.

Here's my current code. I tried using the delay() method but it's not going well.

$(".deltaDrop").hover(function(){
    $('.deltaDrop ul').stop(false,true).slideDown(250);
    $('.delta').css('background-position','-61px -70px');
},function(){
    $('.deltaDrop ul').stop(false,true).slideUp(450);
    $('.delta').css('background-position','-61px 0');
});

Thanks

도움이 되었습니까?

해결책

var timer;
timer =     setTimeout(function () {
                    -- Your code goes here!
                }, 250);

Then you can use the clearTimeout() function like this.

 clearTimeout(timer);

다른 팁

This should work.

$(".deltaDrop").hover(function(){
    $('.deltaDrop ul').stop(false,true).hide(1).delay(250).slideDown();
    $('.delta').css('background-position','-61px -70px');
},function(){
    $('.deltaDrop ul').stop(false,true).show(1).delay(450).slideUp();
    $('.delta').css('background-position','-61px 0');
});

.delay only works when you're dealing with the animation queue. .hide() and .show() without arguments don't interact with the animation queue. By adding the .hide(1) and .show(1) before the .delay() makes the slide animations wait on the queue.

  setTimeout(function() {
    $('.deltaDrop ul').slideDown()
  }, 5000);

Untested, unrefactored:

$(".deltaDrop")
  .hover(
    function()
    {
      var timeout = $(this).data('deltadrop-timeout');

      if(!timeout)
      {
        timeout =
          setTimeout(
            function()
            {
              $('.deltaDrop ul').stop(false,true).slideDown(250);
              $('.delta').css('background-position','-61px -70px');
              $('.deltaDrop').data('deltadrop-timeout', false);
            },
            250
          );
        $(this).data('deltadrop-timeout', timeout);
      }
    },
    function()
    {
      var timeout = $(this).data('deltadrop-timeout');
      if(!!timeout)
      {
        clearTimeout(timeout);
        $('.deltaDrop').data('deltadrop-timeout', false);
      }
      else 
      {
        $('.deltaDrop ul').stop(false,true).slideUp(450);
        $('.delta').css('background-position','-61px 0');
      }
    }
  );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top