js setInterval 有效,但 setTimeout 无效
我正在学习 setTimeout 与 setInterval ,并且我已经让 setInterval 可以工作,但不能让 setTimeout 工作。例如,这不起作用:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Brewery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
function doNothing()
{
var t = "hello wolrd";
}
function poll(){
$.ajax({
//url: "http://192.168.0.11/"+Math.random(),
url:"line-ajax.htm",
contentType:"text/html",
success: 'doNothing()'
});
}
setTimeout(poll(),2000);
</script>
<link rel="stylesheet" type="text/css" href="http://www.highcharts.com/highslide/highslide.css" />
</head>
<body>
<div id="container" style="width: 1200px; height: 500px; margin: 0 auto"></div>
</body>
</html>
但如果我只更改它
setTimeout(poll(),2000);
,
setInterval(poll(),2000);
它将每 2 秒发出一次 ajax 请求...
发生了什么事?
- -编辑 我也尝试过 setTimeout(poll,2000); ,但这也不会每 2 秒发出一次 ajax 请求。
I'm learning about setTimeout vs setInterval and I have got setInterval to work but not setTimeout. for example this doesnt work:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Brewery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
function doNothing()
{
var t = "hello wolrd";
}
function poll(){
$.ajax({
//url: "http://192.168.0.11/"+Math.random(),
url:"line-ajax.htm",
contentType:"text/html",
success: 'doNothing()'
});
}
setTimeout(poll(),2000);
</script>
<link rel="stylesheet" type="text/css" href="http://www.highcharts.com/highslide/highslide.css" />
</head>
<body>
<div id="container" style="width: 1200px; height: 500px; margin: 0 auto"></div>
</body>
</html>
but if i change only this
setTimeout(poll(),2000);
to
setInterval(poll(),2000);
it will make an ajax request every 2 seconds...
what is going on?
---edit
i have also tried setTimeout(poll,2000);
but that doesn't make the ajax request every 2 sec either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除
setTimeout
或setInterval
内的()
。将函数名称视为变量,这就是您要传递的内容。Remove the
()
inside thesetTimeout
orsetInterval
. Treat the function name as a variable, and that's what you're passing..setTimeout()
方法 在指定的延迟之后调用您传递给它的函数一次。.setInterval()
方法 调用您的函数重复传递它,每次调用之间有指定的延迟。从你的问题的更新来看,你似乎认为他们都做同样的事情。请阅读我链接到的文档。
请注意,您必须传递函数引用(或要评估的字符串),因此:
后者不应该起作用,因为它立即调用
poll()
并传递其返回值 (undefined
) 到setInterval()
,所以我真的不明白为什么它对你有用。The
.setTimeout()
method calls the function you pass it exactly once, after the specified delay.The
.setInterval()
method calls the function you pass it repeatedly, with the specified delay between each call.From the update to your question you seem to think they both do the same thing. Please read the doco I've linked to.
Note that you have to pass a function reference (or a string to be eval'd), so say:
The latter should not work because it calls
poll()
immediately and passes its return value (undefined
) tosetInterval()
, so I really can't understand why it worked for you.由于 setTimeout 的参数已被评估,您应该尝试以下操作:
或者,如果您更喜欢使用匿名函数而不是被评估的字符串:
我个人更喜欢后者。
As the parameter of setTimeout is eval'd you should try this:
Or if you like to work with anonymous functions better than with strings that get eval'd:
I personally like the latter more.
setTimeout()
的第一个参数应该是对要调用的函数的引用,而不是使用()
进行的函数调用。所以尝试一下:同样,在
$.ajax()
调用中,success
函数应该是函数指针,而不是字符串:The first argument to
setTimeout()
should be a reference to the function to be called, not a function call with()
. So try:Likewise, in the
$.ajax()
call, thesuccess
function should be a function pointer, not a string: