Javascript setTimeout 在 Chrome 中似乎不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14150802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:51:04  来源:igfitidea点击:

setTimeout doesn't seem to work in Chrome

javascriptgoogle-chromesettimeout

提问by maskedjellybean

This setTimeout works perfectly in Firefox, but in Chrome nothing in function timeoutTrigger ever happens, including the alert. Any ideas?

此 setTimeout 在 Firefox 中完美运行,但在 Chrome 中,函数 timeoutTrigger 中的任何内容都不会发生,包括警报。有任何想法吗?

var $this = $('.active-more');

function timeoutTrigger() {
    $this.closest(".container").nextAll(".container:first").find(".description:first").removeClass('hide');
    $this.closest(".container").nextAll(".container:first").find(".back:first").find("img.portfolio").remove();
    alert("is this thing on?");
}

setTimeout(function(){timeoutTrigger()},400)

采纳答案by Shrey Gupta

Switch your setTimeoutstatement to the following: setTimeout(timeoutTrigger,400);The one you wrote is for when the function you're calling has a parameter. Also, you're missing a semicolon.

将您的setTimeout语句切换为以下语句:setTimeout(timeoutTrigger,400);您编写的语句用于您调用的函数具有参数时。另外,您缺少分号。

回答by Mike G

Google has changed the Content Security Policy which impacts using setTimeout() in some browsers. Please read: https://developer.chrome.com/extensions/contentSecurityPolicy

Google 更改了内容安全策略,这会影响在某些浏览器中使用 setTimeout()。请阅读:https: //developer.chrome.com/extensions/contentSecurityPolicy