brucemand Posted July 20, 2011 Share Posted July 20, 2011 this works; var msg="Thank you for visiting...\n\n BYE!"; alert(msg); but this doesn't; var msg="Thank you for visiting...\n\n BYE!"; var t=setTimeout("alert(msg)",1500); and THIS; var msg="Thank you for visiting...\n\n BYE!"; var t=setTimeout(alert(msg),1500); results in this; Error: useless setTimeout call (missing quotes around argument?)Source File: file:.../###/goodbye.htmLine: 19what am i doing wrong ?EDIT: Error msg for the first failure;Error: msg is not definedSource File: file:.../###/goodbye.htmLine: 19 Link to comment Share on other sites More sharing options...
Synook Posted July 20, 2011 Share Posted July 20, 2011 The first setTimeout call doesn't work, because msg isn't in the right scope, and thus when the argument is evaluated msg is undefined. The second one doesn't work, because by calling alert() at that time you are actually trying to pass the return value of alert() (i.e. nothing) to setTimeout(). This would work: var msg = "...";setTimeout(function() { alert(msg);}, 1500); Link to comment Share on other sites More sharing options...
Ingolme Posted July 20, 2011 Share Posted July 20, 2011 I suggest never using a string as an argument for the setTimeout() and setInterval() methods. Link to comment Share on other sites More sharing options...
brucemand Posted July 20, 2011 Author Share Posted July 20, 2011 I suggest never using a string as an argument for the setTimeout() and setInterval() methods.hmm, okay - then i suggest the w3schools page on said methods be changed accordingly. Link to comment Share on other sites More sharing options...
jeffman Posted July 20, 2011 Share Posted July 20, 2011 The w3schools examples tend to be extremely simplified to you can understand the focus of the tutorial at hand. The explains the unfortunate abundance of document.write statements. So I find myself sympathetic and frustrated at the same time. Link to comment Share on other sites More sharing options...
brucemand Posted July 20, 2011 Author Share Posted July 20, 2011 The w3schools examples tend to be extremely simplified to you can understand the focus of the tutorial at hand. The explains the unfortunate abundance of document.write statements. So I find myself sympathetic and frustrated at the same time.yes, i suppose so.it's the starter kit, or Web Tech 101.i guess it's time i "graduated" to using MDN Docs more often. Link to comment Share on other sites More sharing options...
brucemand Posted July 20, 2011 Author Share Posted July 20, 2011 The first setTimeout call doesn't work, because msg isn't in the right scope, and thus when the argument is evaluated msg is undefined. The second one doesn't work, because by calling alert() at that time you are actually trying to pass the return value of alert() (i.e. nothing) to setTimeout(). This would work:var msg = "...";setTimeout(function() { alert(msg);}, 1500); thanks, i see setTimeout() is just like any other function then.any idea why the tutorials use a var t=setTimeout() to initiate an instance ?'t' never seems to be referenced afterwards. :-/ Link to comment Share on other sites More sharing options...
jeffman Posted July 20, 2011 Share Posted July 20, 2011 If you ever need to cancel the timeout target before it executes, you pass the variable to clearTimeout. So quite often the variable is global. Link to comment Share on other sites More sharing options...
thescientist Posted July 20, 2011 Share Posted July 20, 2011 thanks, i see setTimeout() is just like any other function then.any idea why the tutorials use a var t=setTimeout() to initiate an instance ?'t' never seems to be referenced afterwards. :-/on the same page, in the example for clearInterval(), the t variable is used there to stop the timer. Link to comment Share on other sites More sharing options...
brucemand Posted July 20, 2011 Author Share Posted July 20, 2011 on the same page, in the example for clearInterval(), the t variable is used there to stop the timer.ahh, okay - so unless it needs to be 'clear'-ed, var-ing it is unnecessary. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.