Jump to content

setTimeout syntax error ?


brucemand

Recommended Posts

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: 19
what 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

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...