Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - 包含和不包含引号和括号的setTimeout之间的区别(Difference between setTimeout with and without quotes and parentheses)

I am learning JavaScript and I have learned recently about JavaScript timing events.

(我正在学习JavaScript,最近又了解了JavaScript计时事件。)

When I learned about setTimeout at W3Schools , I noticed a strange figure which I didn't run into before.

(当我在W3Schools了解了setTimeout时,我注意到了一个我从未遇到过的奇怪数字。)

They are using double quotes and then call the function.

(他们使用双引号,然后调用该函数。)

Example:

(例:)

setTimeout("alertMsg()", 3000);

I know that double and single quotes in JavaScript means a string.

(我知道JavaScript中的双引号和单引号表示字符串。)

Also I saw that I can do the same like that:

(我也看到我可以做同样的事情:)

setTimeout(alertMsg, 3000);

With the parentheses it's referring, without the parentheses it's copied.

(带有括号的是指,没有括号的是复制的。)

When I am using the quotes and the parentheses it's getting crazy.

(当我使用引号和括号时,它越来越疯狂了。)

I will be glad if someone can explain to me the difference between these three ways of using setTimeout :

(如果有人可以向我解释这三种使用setTimeout方式之间的区别,我将感到非常高兴:)

With the parentheses:

(加上括号:)

setTimeout("alertMsg()", 3000);

Without the quotes and the parentheses:

(没有引号和括号:)

setTimeout(alertMsg, 3000);

And the third is only using quotes:

(第三只使用引号:)

setTimeout("alertMsg", 3000);

NB: A better source for setTimeout reference would be MDN .

(注意: setTimeout引用的更好来源是MDN 。)

  ask by user1316123 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using setInterval or setTimeout(使用setIntervalsetTimeout)

You should pass a reference to a function as the first argument for setTimeout or setInterval .

(您应该将对函数的引用作为setTimeoutsetInterval的第一个参数传递。)

This reference may be in the form of:

(该参考可以采用以下形式:)

  • An anonymous function

    (匿名功能)

     setTimeout(function(){/* Look mah! No name! */},2000); 
  • A name of an existing function

    (现有功能的名称)

     function foo(){...} setTimeout(foo, 2000); 
  • A variable that points to an existing function

    (指向现有功能的变量)

     var foo = function(){...}; setTimeout(foo, 2000); 

    Do note that I set "variable in a function" separately from "function name".

    (请注意,我将“函数中的变量”与“函数名”分开设置。)

    It's not apparent that variables and function names occupy the same namespace and can clobber each other.

    (变量和函数名占用相同的名称空间并相互破坏可能并不明显。)

Passing arguments(传递参数)

To call a function and pass parameters, you can call the function inside the callback assigned to the timer:

(要调用函数并传递参数,可以在分配给计时器的回调内部调用函数:)

setTimeout(function(){
  foo(arg1, arg2, ...argN);
}, 1000);

There is another method to pass in arguments into the handler, however it's not cross-browser compatible .

(还有另一种将参数传递给处理程序的方法,但是它与跨浏览器不兼容 。)

setTimeout(foo, 2000, arg1, arg2, ...argN);

Callback context(回调上下文)

By default, the context of the callback (the value of this inside the function called by the timer) when executed is the global object window .

(默认情况下,回调的上下文中的值( this在执行时由定时器调用的函数内)是全局对象window 。)

Should you want to change it, use bind .

(如果要更改它,请使用bind 。)

setTimeout(function(){
  this === YOUR_CONTEXT; // true
}.bind(YOUR_CONTEXT), 2000);

Security(安全)

Although it's possible, you should not pass a string to setTimeout or setInterval .

(尽管有可能,但您不应将字符串传递setTimeoutsetInterval 。)

Passing a string makes setTimeout() or setInterval() use a functionality similar to eval() that executes strings as scripts , making arbitrary and potentially harmful script execution possible.

(传递字符串使setTimeout()setInterval()使用类似于eval()的功能,该功能将字符串作为脚本执行 ,从而可以执行任意的且可能有害的脚本。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...