SetTimeout和setInterval第一个参数,不带括号,不带双引号 [英] SetTimeout and setInterval the first parameter plus no parentheses, plus no double quotes

查看:296
本文介绍了SetTimeout和setInterval第一个参数,不带括号,不带双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var num=0;
function numCount(){
 document.getElementById('txt').value=num;
 num=num+1;
 setTimeout(numCount(),1000);
 }

</script>
</head>
<body>
<form>
<input type="text" id="txt" />
<input type="button" value="Start" onClick="numCount()" />
</form>
</body>
</html>

为什么要在上述代码的第一个参数中添加括号,为什么要在seTimeout中添加双引号?那么setInterval呢?

Why do you want to add parentheses to the first parameter in the above code, and why do you add a double quotation mark in the seTimeout? What about setInterval?

推荐答案

将其更改为:

setTimeout(numCount,1000);

使用()可以立即调用该功能.上面是通过传递函数作为参考的,它将在延迟时间之后调用.

With the () you are invoking the function immediately. Above is passing the function as a reference and it will be called after the delay time.

另一种不太喜欢使用引号的方式是:

The other less preferred way using quotes is:

setTimeout('numCount()',1000); // or setTimeout("numCount()",1000);

使用字符串参数..字符串将在计时器延迟结束时作为脚本进行评估

Using string argument.. the string will be evaluated as script at end of timer delay

如有疑问,请在 MDN文档中进行查找

这篇关于SetTimeout和setInterval第一个参数,不带括号,不带双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆