简体   繁体   English

Javascript:window.setTimeout()不起作用

[英]Javascript: window.setTimeout( ) not working

I feel like this must be very simple, but for some reason I am getting no results from this: 我觉得这一定很简单,但是由于某些原因,我没有得到任何结果:

<script>
function sendAlert() {
    alert('hello world');
}
var timer = window.setTimeout(sendAlert, 2000);
</script>

Should this not be counting 2000ms and then calling sendAlert() ? 这不应该计算2000ms然后调用sendAlert()吗? Have I understood this wrong? 我了解这个错误吗? I have also tried this outside of a variable (ie. window.setTimeout() not declared as var timer ) and had no result either. 我也在变量之外尝试过此操作(即未声明为var timer window.setTimeout() ),也没有任何结果。 This seems so easy, what am I doing wrong!? 这似乎很容易,我在做什么错!!


UPDATE: 更新:

I'm an idiot. 我是个白痴。 I had script tags with bad links to javascript documents in the header of my doc, breaking javascript in general on the page. 我的文档标题中的脚本标签带有指向javascript文档的错误链接,从而破坏了页面上的javascript。 Thanks internet. 谢谢互联网。 ;p ; p

OP just updated his post: OP刚刚更新了他的帖子:

I'm an idiot. 我是个白痴。 I had script tags with bad links to javascript documents in the header of my doc, breaking javascript in general on the page. 我的文档标题中的脚本标签带有指向javascript文档的错误链接,从而破坏了页面上的javascript。 Thanks internet. 谢谢互联网。 ;p ; p

Well, that explains it. 好吧,这可以解释。 Always check your error console! 始终检查您的错误控制台! :) :)

The javascript is correct. javascript是正确的。 However, the tag might give problems. 但是,标签可能会出现问题。 Try this: 尝试这个:

<script type="text/javascript">
function sendAlert() {
    alert('hello world');
}
var timer = window.setTimeout(sendAlert, 2000);
</script>

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

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