user3350511
user3350511

Reputation: 61

Can we pause a delay in jQuery?

I have a button (with an ID proceed) which is set to display:none. After clicking on the Vimeo "play" button, this button appears with a delay of 10sec.

Is it possible to pause the delay after clicking on "pause" in the video, so the timer stops and start the timer again by clicking on "play" again?

This is what I have so far (see also this fiddle):

<script>
    var iframe = document.querySelector('iframe');
    var player = new Vimeo.Player(iframe);

    player.on('play', function() {
  jQuery('#proceed').delay(10000).show(0);   
    });
</script>

Upvotes: 2

Views: 269

Answers (2)

Mitya
Mitya

Reputation: 34556

You can't pause a jQuery .delay(). But you can if you do things manually with intervals:

//prep
let
int,
secs = 10,
elapsed = 0
;

//on play, start/resume interval timer - when we get to 10, show button
player.on('play', () => {
    int = setInterval(() => {
        elapsed++;
        if (elapsed == secs) {
            jQuery('#proceed').show();
            clearInterval(int);
        }
    }, 1000);
});

//pause interval timer on pause
player.on('pause', () => {
    clearInterval(int);
});

You'll probably want to reset the interval completely if you restart the video or move the playhead back to 0 secs.

Upvotes: 2

Max
Max

Reputation: 1025

No.

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.

https://api.jquery.com/delay/

Upvotes: 0

Related Questions