为什么 setTimeout 会“排队”?调用 stopTimeout 时回调?

发布于 2024-12-27 02:34:24 字数 641 浏览 1 评论 0原文

我创建了一个滑块,它可以与下一个/上一个箭头一起用于计时器。

单击箭头时,我想停止自动计时器,然后在最后一次单击后重新启动 x-time。不幸的是,我目前所拥有的似乎是计时器,所以如果多次单击箭头,自动计时器会重新启动,但移动速度非常快......

我似乎无法解决这个问题 - 如何仅维护一个 setInterval 并避免它们累积。 ..

非常感谢任何帮助 - 下面粘贴的代码

    var int = setInterval(back, autoTimerTime);
    //down arrow    
    $('.arrow-down').click(function(){
        if (!$('ul.as-thumbs').is(':animated')) {
            back();
            clearInterval(int);
            clearTimeout(timeout);
            var timeout = setTimeout(function() {
                var int = setInterval(back, autoTimerTime);
            }, 8000);
        }
    });

I've created a slider which works on a timer along with next/prev arrows.

When an arrow is clicked I want to stop the auto timer then re-start x-time after the last click. Unfortunately what I have currently seems to que timers so if the arrow is clicked multiple times the auto timer restarts but moves really fast...

I can't seem to work it out - how to maintain just one setInterval and avoid them building up...

Any help greatly appreciated - code pasted below

    var int = setInterval(back, autoTimerTime);
    //down arrow    
    $('.arrow-down').click(function(){
        if (!$('ul.as-thumbs').is(':animated')) {
            back();
            clearInterval(int);
            clearTimeout(timeout);
            var timeout = setTimeout(function() {
                var int = setInterval(back, autoTimerTime);
            }, 8000);
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2025-01-03 02:34:24

您必须将对 timout 的引用放置在 click 处理程序的公共范围内,如下所示。当在新作用域中使用 var 时,会在本地作用域[1] 中再次声明该变量。

var int = setInterval(back, autoTimerTime);
var timeout; // This variable is now shared by all $('.arrow-down').click funcs
//down arrow    
$('.arrow-down').click(function(){
    if (!$('ul.as-thumbs').is(':animated')) {
        back();
        clearInterval(int);
        clearTimeout(timeout);
        timeout = setTimeout(function() {
            // Removed `var` too
            int = setInterval(back, autoTimerTime);
        }, 8000);
    }
});

[1]:局部/全局变量图解简而言之

,以var关键字为前缀的变量在局部作用域中再次声明。在 JavaScript 中,可以通过使用 function() { .. } 包围一个块来创建新作用域。

当请求变量时,引擎首先在当前(本地)范围中查找。如果该变量存在,则使用该变量。
否则,将检查父作用域等,直到找到变量。如果在顶部(全局范围)没有找到该变量,则会发生以下情况:

  • 在严格模式下,将抛出 ReferenceError
  • 赋值时,foo = 1,该变量将在全局范围内声明

    @Nitpicks:不考虑 let
  • 读取时:将抛出 ReferenceError
var int = 1, timeout = 2, homeless = 3;
function click() {
    var timeout = 1;
    homeless = 4;
    timeout = function() {
        var int = 2;
    }
}
click();

Variables in the global scope:
- int     (declared using var, inititalized at 1)
- timeout (declared using var, inititalized at 2)
- homeless(declared using var, initialized at 3)
--- New scope of: function click()
  - No declaratation of `int`, so if used, `int` refers to the global `int`
  - Global var: homeless  (assigned 4)
  - Local var: timeout    (declared using var, init at 1)
  - Local var: timeout (assigned anonymou function())
  --- New scope of: function()
    - Local var: int     (declared using var, init at 1)
    - Accessible vars from parent scope: timeout
    - Accessible vars from global scope: homeless, int
      (Note that the global `timeout` var is unreachable, because it
       has been redeclared at the parent scope)

You have to place the reference to the timout in the common scope of the click handlers, as shown below. When var is used in a new scope, the variable is declared again, in the local scope[1].

var int = setInterval(back, autoTimerTime);
var timeout; // This variable is now shared by all $('.arrow-down').click funcs
//down arrow    
$('.arrow-down').click(function(){
    if (!$('ul.as-thumbs').is(':animated')) {
        back();
        clearInterval(int);
        clearTimeout(timeout);
        timeout = setTimeout(function() {
            // Removed `var` too
            int = setInterval(back, autoTimerTime);
        }, 8000);
    }
});

[1]: Illustrated explanation of local/global variables

In short, variables prefixed by the var keyword are declared again in the local scope. In JavaScript new scope can be created by surrounding a block by function() { .. }.

When a variable is requested, the engine first looks in the current (local) scope. If the variable is present, this variable is used.
Otherwise, the parent scope is examined, etc, etc, until the variable is found. If the variable is not found at the top (global scope), the following will happen:

  • In strict mode, a ReferenceError will be thrown.
  • When assigning, foo = 1, the variable will be declared at the global scope

    @Nitpicks: let not taken into account)
  • When reading: A ReferenceError will be thrown.
var int = 1, timeout = 2, homeless = 3;
function click() {
    var timeout = 1;
    homeless = 4;
    timeout = function() {
        var int = 2;
    }
}
click();

Variables in the global scope:
- int     (declared using var, inititalized at 1)
- timeout (declared using var, inititalized at 2)
- homeless(declared using var, initialized at 3)
--- New scope of: function click()
  - No declaratation of `int`, so if used, `int` refers to the global `int`
  - Global var: homeless  (assigned 4)
  - Local var: timeout    (declared using var, init at 1)
  - Local var: timeout (assigned anonymou function())
  --- New scope of: function()
    - Local var: int     (declared using var, init at 1)
    - Accessible vars from parent scope: timeout
    - Accessible vars from global scope: homeless, int
      (Note that the global `timeout` var is unreachable, because it
       has been redeclared at the parent scope)
救赎№ 2025-01-03 02:34:24

clearTimeout(timeout); 永远不会起作用,因为“timeout”不是全局变量。您在函数内定义它。

clearTimeout(timeout); will never work because "timeout" is not a global variable. You're defining it WITHIN the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文