简体   繁体   English

在将函数作为参数传递时使用字符串

[英]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.

相关问题 为什么将 function 参数作为字符串或引用传递时,“this”会发生变化? - Why does `this` change when passing the function argument as string or reference? 将字符串参数传递给javascript函数 - Passing a string argument to a javascript function 将函数作为参数传递时,JavaScript“不是函数”错误 - JavaScript “ is not a function” error when passing function as argument 将字符串参数传递给jQuery,并认为其功能 - Passing string argument to jQuery and it thinks its function 将字符串参数传递给onclick javascript函数 - passing string argument to onclick javascript function 使用字符串作为函数的参数 (JavaScript) - Use string as argument of a function (JavaScript) 将函数作为参数传递给curried函数时,有什么方法可以提取此参数? - Is there any way to extract this argument when passing a function as an argument to a curried function? Javascript,在传递函数参数时保留此引用 - Javascript, preserving this reference when passing function argument 将参数传递给函数时将字符串转换为数组 - Converting a sting into an array when passing argument into a function 将参数传递给coffeescript中带有`myarguments ...`的函数时出现问题 - issue when passing argument to a function with `myarguments…` in coffeescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM