First off, let's get straight to the point on something here: Providing a string as the primary contention to setTimeout or setInterval isn't itself an oversight in essence. It is superbly real JavaScript code. The issue here is more one of execution and productivity. What is seldom clarified is that, in the engine, on the off chance that you go in a string as the main contention to setTimeout or setInterval, it will be passed to the capacity constructor to be changed over into another capacity. This procedure can be moderate and wasteful, and is seldom fundamental.
The other option to passing a string as the primary contention to these techniques is to rather go in a capacity. We should investigate an illustration.
Here, at that point, would be a genuinely run of the mill utilization of setInterval and setTimeout, passing a string as the main parameter:
setInterval("logTime()", 1000);
setTimeout("logMessage('" + msgValue + "')", 1000);
The better decision is go in a capacity as the underlying contention; e.g.:
setInterval(logTime, 1000); // passing the logTime function to setInterval
setTimeout(function() { // passing an anonymous function to setTimeout
logMessage(msgValue); // (msgValue is still accessible in this scope)
}, 1000);
0 comments:
Post a Comment