我们可以在 jQuery 暂停延迟吗?
Can we pause a delay in jQuery?
我有一个设置为 display:none 的按钮(带有 ID 继续)。单击 Vimeo "play" 按钮后,此按钮会延迟 10 秒出现。
是否可以在点击视频中的"pause"后暂停延迟,让计时器停止并再次点击"play"重新开始计时?
这是我目前所拥有的(另见 fiddle):
<script>
var iframe = document.querySelector('iframe');
var player = new Vimeo.Player(iframe);
player.on('play', function() {
jQuery('#proceed').delay(10000).show(0);
});
</script>
没有。
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.
您无法暂停 jQuery .delay()
。但是,如果您按间隔手动执行操作,则可以:
//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);
});
如果您重新开始播放视频或将播放头移回 0 秒,您可能需要完全重置间隔。
我有一个设置为 display:none 的按钮(带有 ID 继续)。单击 Vimeo "play" 按钮后,此按钮会延迟 10 秒出现。
是否可以在点击视频中的"pause"后暂停延迟,让计时器停止并再次点击"play"重新开始计时?
这是我目前所拥有的(另见 fiddle):
<script>
var iframe = document.querySelector('iframe');
var player = new Vimeo.Player(iframe);
player.on('play', function() {
jQuery('#proceed').delay(10000).show(0);
});
</script>
没有。
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.
您无法暂停 jQuery .delay()
。但是,如果您按间隔手动执行操作,则可以:
//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);
});
如果您重新开始播放视频或将播放头移回 0 秒,您可能需要完全重置间隔。