Javascript: Uncaught TypeError: window.setTimeout(...) is not a function

Javascript: Uncaught TypeError: window.setTimeout(...) is not a function

我在 setTimeout() 中包装了一个 Javascript 函数,我通过 Google 标签管理器在网站上 运行 这段代码。它有效 - 在代码执行之前有 3000 毫秒的延迟,它为我解决了一个问题。

但是,当我在 google chrome 上进入 Javascript 控制台时,我每次执行此函数时都会看到 Uncaught TypeError: window.setTimeout(...) is not a function

下面是我的代码的最小化版本:

<script>
window.setTimeout(function() {

    function eventHandler(e) {
        //Code here
    }

        //Code here
        item[i].addEventListener("event", eventHandler, false);

    }
}, 3000)();
</script>

知道为什么会抛出此错误或如何修复它吗?

提前致谢!

基本上是因为 window.setTimeout return 是一个标识符而不是 return 一个函数,所以将您的 window.setTimeout 调用替换为:

<script>
var id = window.setTimeout(function() {

    function eventHandler(e) {
        //Code here
    }

        //Code here
        item[i].addEventListener("event", eventHandler, false);

    }
}, 3000);
</script>