![](/img/trans.png)
[英]after executing, window.setTimeout() throws 'Uncaught TypeError: undefined is not a function'?
[英]Javascript: Uncaught TypeError: window.setTimeout(…) is not a function
我已经在 setTimeout() 中包装了 Javascript function 并且我正在通过 Google 跟踪代码管理器在网站上运行此代码。 它有效 - 在代码执行之前有 3000 毫秒的延迟,它为我解决了一个问题。
However, when I go into the Javascript console on google chrome, I see Uncaught TypeError: window.setTimeout(...) is not a function
for every time this function executes.
下面是我的代码的最小化版本:
<script>
window.setTimeout(function() {
function eventHandler(e) {
//Code here
}
//Code here
item[i].addEventListener("event", eventHandler, false);
}
}, 3000)();
</script>
有什么想法为什么会引发此错误或如何解决?
先感谢您!
基本上是因为window.setTimeout
返回一个标识符号并且不返回 function,所以将您的window.setTimeout
调用替换为:
<script>
var id = window.setTimeout(function() {
function eventHandler(e) {
//Code here
}
//Code here
item[i].addEventListener("event", eventHandler, false);
}
}, 3000);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.