jQuery delay() - how to stop it?

后端 未结 4 902
滥情空心
滥情空心 2020-12-03 13:58

I already tried stop(true,true), stop(true) and clearQueue(); but this doesn\'t work.

I have problem with fast changing slides, i already have some function what hav

相关标签:
4条回答
  • 2020-12-03 14:13

    I wanted a user to rapidly restart an animation chain over and over again, sometimes in the middle of the animation. So instead of using delay() I animated to a bogus css propery {"null":1}. Here's a simple fade-in/fade-out. Seems to have worked for me!

    //- fade in
    $el.stop().animate({"opacity":1}, 200, "easeInSine", function(){
        //- delay for 2000ms
        $el.stop().animate({"null":1}, 2000, function(){
            //- fade out
            $el.stop().animate({"opacity":0}, 1000, "easeInOutSine", function(){
                //- final callback
            });
        });
    });
    
    0 讨论(0)
  • 2020-12-03 14:15

    From the jQuery delay page:

    The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

    Take a look at the doTimeout plugin; it may be what you are looking for.

    I hope this helps!

    0 讨论(0)
  • You can break .delay() by .dequeue() function

    here is example

    //this is only for make sure that we skip 'delay', not other function
    var inDelay = false;
    
    function start()
    {
        $('.gfx').animate
        ({
            width: 100
        }, function(){inDelay = true}).delay(3000).animate
        ({
            width: 0
        }, function(){inDelay = false})
    }
    
    function breakTheDelay()
    {
        if(inDelay)
        {
            $('.gfx').dequeue();
        }
    }
    

    http://jsfiddle.net/wasikuss/5288z/

    //edit: more complex example provided with logging timestamps and cleanup ( without cleanup, multiple clicks on Start is unpredictable ) to prove that it works

    http://jsfiddle.net/q0058whc/

    or below

    //this is only for make sure that we skip 'delay', not other function
    var inDelay = false;
    var ltime = 0;
    
    // console log will aways show 'end' values: 2000, 1000 an 400
    // values may be different due to time wasted on calling functions
    // sometimes delay is shorten by 0-200ms, it can be caused by js engine probably  
    
    function start()
    {
        cleanup();
        ltime = (1*new Date());
        $('.gfx').queue('fx', function(next)
        {
          logtime( 'animate1_start' );
        	$('.gfx').animate
          ({
              width: 100
          }, 2000, function(){logtime('animate1_end');inDelay = true;})
          next();
        })
        .queue('fx', function(next)
        {
        	logtime('delay_start');
        	next()
        })
        .delay(1000)
        .queue('fx', function(next)
        {
        	logtime('delay_end');
        	next()
        })
        .queue('fx', function(next)
        {
        	logtime('animate0_start');
        	$('.gfx').animate
          ({
              width: 0
          }, function(){logtime('animate0_end');inDelay = false;})
          next()
        });
    }
    
    function cleanup()
    {
    		// remove current queue
        $('.gfx').clearQueue()
        // first animate runned interval. stop it 
        .stop()
        // reset width of element
        .css('width',0)
    }
    
    function logtime( name )
    {
    	var ntime = (1*new Date());
    	console.log( name + ': ' + ( ntime - ltime ) );
      ltime = ntime;
    }
    
    function breakTheDelay()
    {
        if(inDelay)
        {
            $('.gfx').dequeue();
        }
    }
    
    //
    // version without 'inDelay' check only if you know what are you doing
    // http://jsfiddle.net/wasikuss/hkw9H/
    //
    .gfx
    {
        background: red;
        width: 0;
        height: 10px
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <button onclick="start()">Start</button>
    <button onclick="breakTheDelay()">Can't wait!</button>
    <div class="gfx"></div>

    0 讨论(0)
  • 2020-12-03 14:28

    Via this post there's a very simple approach, which is to use .animate in place of .delay

    0 讨论(0)
提交回复
热议问题