using setTimeout() and .call()

using setTimeout() and .call()

本文关键字:call and setTimeout using      更新时间:2023-09-26

我有点坚持尝试将 setTimeout 设置为使用 .call() 方法进行的函数调用。

基本上,我有一个函数引用数组,然后逐个遍历它们,并以一定的 setTimeout 间隔调用它们。第一个函数触发了 aok,但第二个函数没有,我在 js 控制台中收到一个我不明白的错误 - 错误是 -

未捕获的类型错误:对象 73 没有方法"调用"

代码:

        function scene1(){             
            alert("boo");
        }
        function scene2(){          
            alert("boo2");
        }            
        var arrAnimation = [];
        arrAnimation[0] = scene1;                   
        arrAnimation[1] = scene2;                 
        //step through the array
        for (var i = 0; i < arrAnimation.length; i++){
                setTimeout(arrAnimation[i],3000).call();                  
        }  

任何帮助将不胜感激。Dan

你不需要使用 call - 你需要做的就是将函数传递给setTimeout,它将自动运行:

setTimeout(arrAnimation[i],3000);

仅供参考,错误本身 - setTimeout返回一个数字作为句柄,如果需要,可以稍后取消超时。 数字没有call功能。

.call方法放在错误的位置。 给你:

http://jsfiddle.net/JkLk2/