[英]use of string when passing function as argument
The following script (courtesy of Adam Khoury ) creates a timer which gives a message upon completion. 以下脚本(由Adam Khoury提供 )创建了一个计时器,该计时器在完成时给出一条消息。 The functionality makes sense to me, but I am struggling to understand the use of strings here.
该功能对我来说很有意义,但是我在这里很难理解字符串的用法。 Particularly:
尤其:
1) Why must 'countDown('+secs+',"'+elem+'")'
be passed as a string? 1)为什么必须将
'countDown('+secs+',"'+elem+'")'
作为字符串传递? In other examples I have seen, setTimeout can accept a function (without 'quotes'). 在其他示例中,setTimeout可以接受一个函数(不带“引号”)。
2) Likewise, in that same line, why must elem
be passed as a string (using "quotes")? 2)同样,在同一行中,为什么必须将
elem
作为字符串传递(使用“引号”)? It seems that the elem
variable already holds a string value, the name of the id ( "status"
) 似乎
elem
变量已经包含一个字符串值,即id的名称( "status"
)
If you have any light to shed on this, or misconceptions to correct, I'll be grateful! 如果您对此有任何了解,或者有错误的观念可以纠正,我将不胜感激!
<script type="text/javascript">
function countDown(secs,elem) {
var element = document.getElementById(elem);
element.innerHTML = "Please wait for "+secs+" seconds";
if(secs < 1) {
clearTimeout(timer);
element.innerHTML = '<h2>Countdown Complete!</h2>';
}
secs--;
var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
</script>
<div id="status"></div>
<script type="text/javascript">countDown(10,"status");</script>
Why must
'countDown('+secs+',"'+elem+'")'
be passed as a string?为什么必须将
'countDown('+secs+',"'+elem+'")'
作为字符串传递?
It doesn't have to be passed like that. 不必那样传递。 Although
setTimeout
accepts a string, you should avoid passing a string. 尽管
setTimeout
接受字符串,但您应避免传递字符串。 You will see its limitations quickly when you somehow try to call the callback function with an object. 当您以某种方式尝试使用对象调用回调函数时,将很快看到其局限性。 Passing a function is a much better approach.
传递函数是一种更好的方法。
Likewise, in that same line, why must elem be passed as a string (using "quotes")?
同样,在同一行中,为什么必须将elem作为字符串传递(使用“引号”)? It seems that the elem variable already holds a string value, the name of the id ("status")
似乎elem变量已经包含一个字符串值,即id的名称(“状态”)
That's right, elem
is already a string, but if you omit the quotes, you will create a string such as 是的,
elem
已经是一个字符串,但是如果省略引号,则会创建一个字符串,例如
'countDown(10, status)'
If setTimeout
is evaluating the string later on, it will try to access for a variable status
, which does not exist. 如果
setTimeout
稍后评估字符串,它将尝试访问变量status
,该status
不存在。 That's why you want to the final string to look like 这就是为什么您想要最终的字符串看起来像
'countDown(10, "status")'
and for that you have to add the quotation marks. 为此,您必须添加引号。
So, a cleaner implementation would be 因此,更清洁的实施方式是
var timer = setTimeout(function() {
countDown(secs, elem);
}, 1000);
Note that the code is actually not working correctly. 请注意,该代码实际上无法正常工作。 The
timer
variable is wrongly scoped, doesn't have any effect and setTimeout
will still be called even if secs < 1
. timer
变量的作用域错误,没有任何效果,即使secs < 1
仍将调用setTimeout
。
It should probably be like this: 可能应该是这样的:
function countDown(secs,elem) {
var element = document.getElementById(elem);
if(secs < 1) {
element.innerHTML = '<h2>Countdown Complete!</h2>';
return;
}
else {
element.innerHTML = "Please wait for "+secs+" seconds";
}
setTimeout(function() {
countDown(secs - 1, elem);
}, 1000);
}
best practice 最佳实践
setTimeout(function(){
countDown(secs,elem);
},3000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.