简体   繁体   English

Javascript:由于第3方,我的setTimeout函数不起作用

[英]Javascript: My setTimeout function not working because of 3rd party

I have intergrated a third party code into my website, and suddenly the setTimeout and setInterval stopped working: Before the third party is loaded, everything works fine. 我已经将第三方代码整合到我的网站中,突然setTimeout和setInterval停止工作:在加载第三方之前,一切正常。 setTimeout and setInterval that were scheduled to run after the third party is loaded, do not dun at all. 计划在加载第三方之后运行的setTimeout和setInterval根本不起作用。 After removing the 3rd party code snippet they supplied, everything work. 删除他们提供的第三方代码段后,一切正常。

My question is - how can it be? 我的问题是-怎么可能? what can the third pary do that can stop my schedules? 第三个伙伴可以做什么来阻止我的日程安排? I renamed any pointers to the returned value of the setTimeout function; 我将所有指针重命名为setTimeout函数的返回值; I tried to play with the place where I put the snippet and / or the setTimeout code. 我尝试在放置代码段和/或setTimeout代码的地方玩。 Nothing work. 没事

It doesn't make any sense. 这没有任何意义。 and I don't know how to start debugging it. 而且我不知道如何开始调试它。

Here is a simplified html: 这是一个简化的html:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Demo</title>
<link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico"/>
<!--<link href="css/style.css" rel="stylesheet" type="text/css">-->
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<script>
<!-- the alert pops up only when I remove the 3rd party snippet -->
    setTimeout(function(){      
        alert("done");
    }, 5000);   
</script>

<!-- This is the 3rd party code -->
<script type="text/javascript">
    var $P; var prefix = ('https:' == document.location.protocol ? 'https://' : 'http://'); var         _P = _P || [];
    _P.push(['setId', '123']);
    var PPP = document.createElement('script');
    PPP.type = 'text/javascript'; PPP.src = prefix +     'thethirdpartyIuse.com/functions.js';
    var s = document.getElementsByTagName('script');
    var lastScriptTag = s[s.length - 1];
    lastScriptTag.parentNode.insertBefore(PPP, lastScriptTag);
</script>
</body>
</html>

Any help & guideness is appriciated! 任何帮助和指导都适用!

Are you sure? 你确定吗? It works fine in liveweave: http://liveweave.com/OKMyjs And indeed you should wrap your code in: 它可以在liveweave中正常工作: http ://liveweave.com/OKMyjs而且确实应该将代码包装在:

 window.onload = function() {

//your stuff

}

May be the external code redefines alert as something else. 可能是外部代码将alert重新定义为其他内容。 To see if this is the issue try changing your code to: 若要查看是否是问题,请尝试将代码更改为:

(function(myalert){
  setTimeout(function(){      
      myalert("done");
  }, 5000);
})(alert);

If this doesn't work the only reason I can think to is that the external script goes into an infinite loop. 如果这不起作用,我可以想到的唯一原因是外部脚本进入了无限循环。 Functions registered with setTimeout will be executed once the Javascript event loop starts after the synchronous execution of script tags in the page and this may never happen if any toplevel stript hangs. 在页面中script标记的同步执行之后,一旦Javascript事件循环开始,则将执行在setTimeout注册的函数,并且如果挂起任何顶级剥离,则可能永远不会发生。

You should however see in this case the loading is taking forever (loading animation in the browser) and after long enough the browser should signal that there is a problem loading the page. 但是,在这种情况下,您应该看到加载将永久进行(在浏览器中加载动画),并且在浏览足够长时间后,浏览器应发出信号,表明加载页面存在问题。

Something redefines your alert function. 某些东西重新定义了您的alert功能。

Possible solutions: 可能的解决方案:

  • wrap your code in (function(alert) { /* your code */ }(alert)) 将您的代码包装在(function(alert) { /* your code */ }(alert))
  • Object.defineProperty(window, 'alert', { configurable: false, enumerable: true, value: alert, writable: false }); to protect your alert from redefining. 以保护您的alert免于重新定义。
  • do not rely on alert (in 99% of cases modal or console.log are superior solutions - alert , prompt and confirm are three functions blocking code execution in JavaScript). 不要依赖alert (在99%的情况下,modal或console.log是更好的解决方案- alertpromptconfirm是阻止JavaScript中代码执行的三个功能)。

After a lot of debugging, I found the probelm: The 3rd party did setTimeout(true) for some reason (I assume by mistake). 经过大量调试后,我发现了问题:第三方出于某种原因(我认为是错误的)做了setTimeout(true) )。 In chrome, it cancel all schedules ( setTimeout & setInterva ). 在chrome中,它将取消所有计划( setTimeoutsetInterva )。

I report the problem. 我报告了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM