views:

61

answers:

4

Hi, 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

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

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

 clearTimeout(timer);
Frej
Excellent! That's just what I needed!
MaxH
Sorry I can't 'up' your answer but I don't have enough Rep.
MaxH
Just glad I could help. =)
Frej
A: 

You can use setTimeout().

bcmcfc
A: 
  setTimeout(function() {
    $('.deltaDrop ul').slideDown()
  }, 5000);
Júlio Santos
A: 

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');
      }
    }
  );
Thomas