为什么清除超时不起作用

why clear timeout is not working ?

本文关键字:不起作用 超时 清除 为什么      更新时间:2023-09-26

我像一个循环一样用5秒来调用一个函数。这是我的代码

var timeouts = [];
   res.forEach(function (entry) {
     timeouts[entry] = [];
   });
   for (var entry in new_image_list) {
    for(var newentry in new_image_list[entry])
    {           {
          timeouts[entry][newentry] =setTimeout('load_image_file(' + entry + ',' + newentry          + ')', timer * 5000);
    timer++;
}
}

如果点击了取消循环按钮

功能内容类似

for (var entry in timeouts) {
    for(var newentry in timeouts[entry])
    {
        clearTimeout(timeouts[entry][newentry]);
    }
}

我正在尝试使用clearTimeout函数清除超时。但为什么它不起作用呢?

试试这个。。。

clearTimeout(timeouts[entry][newentry])更改为clearTimeout(newentry)