...

View Full Version : setTimeout("FuncName", 500); gets is not defined



htizo
01-18-2009, 11:35 AM
G'day All,
I'm checking to see if a script has been loaded.
I have set
var externalScriptLoaded = true;
in the script being loaded.


checkScriptLoaded;
function checkScriptLoaded() {
if(!externalScriptLoaded) {
alert("BooHoo it's NOT loaded");
setTimeout("checkScriptLoaded", 500);
} else {
// script loaded and ready to use
alert("Hoorah it's loaded");
}
}


However I get "checkScriptLoaded is not defined"

Can someone tell me why?

Thanking you in anticipation,
Htizo

Philip M
01-18-2009, 11:38 AM
setTimeout("checkScriptLoaded()", 500);


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

itsallkizza
01-18-2009, 06:28 PM
Treat the first parameter like an eval().

htizo
01-18-2009, 10:03 PM
G'day,
Thank you both for your replies.

I tried :
setTimeout("checkScriptLoaded()", 500);
same message
"checkScriptLoaded is not defined"


Treat the first parameter like an eval().
I'm afraid I don't understand that :-(
(I'll do a google now.)
Could you explain?

Thanks,
Htizo

Philip M
01-18-2009, 10:16 PM
setTimeout("checkScriptLoaded()", 500);

htizo
01-18-2009, 10:23 PM
G'day Philip,

Sorry mate typo, I did try:
setTimeout("checkScriptLoaded()", 500);
this time i copied from the editor.

Thanks for taking the time to reply!
Any other ideas?

Thanks and sorry mate,
Htizo

flynch01
01-18-2009, 11:20 PM
Could it maybe be that you're calling it without any parenthesis? Errors in red:



checkScriptLoaded;
function checkScriptLoaded() {
if(!externalScriptLoaded) {
alert("BooHoo it's NOT loaded");
setTimeout("checkScriptLoaded", 500);
} else {
// script loaded and ready to use
alert("Hoorah it's loaded");
}
}


Fixed:


checkScriptLoaded();
function checkScriptLoaded() {
if(!externalScriptLoaded) {
alert("BooHoo it's NOT loaded");
setTimeout("checkScriptLoaded()", 500);
} else {
// script loaded and ready to use
alert("Hoorah it's loaded");
}
}

For the record, you can actually do this in setTimeout


setTimeout( checkScriptLoaded, 500 )

This works because you aren't calling checkScriptLoaded, you are passing the signature of the function. It's very slightly more efficient to pass the function signature like this because passing it as a string means the function has to parse the string to match the function. Only time you need to use a string would be if you want to pass an argument through it, like "checkScriptLoaded( argument1, argument 2 )".

Hope that helps.

htizo
01-18-2009, 11:56 PM
For the record, you can actually do this in setTimeout

setTimeout( checkScriptLoaded, 500 )
This works because you aren't calling checkScriptLoaded, you are passing the signature of the function. It's very slightly more efficient to pass the function signature like this because passing it as a string means the function has to parse the string to match the function. Only time you need to use a string would be if you want to pass an argument through it, like "checkScriptLoaded( argument1, argument 2 )".

Hope that helps.

It does !
I tried all the others above and they give the same error however when I removed the quotes it works.
THANK YOU.

However the variable set true in the script being loaded is not returning true.
More work to do :-)

Thanks to all who replied,
Htizo

htizo
01-19-2009, 12:28 AM
G'day All,
Thanks to all but particularly flynch01 as I now have a working script.
This is what worked. The fail safe is important!



//In the script I'm loading, sb.js I set the variable "var externalScriptLoaded = true;"
//for some reason I need to set the variable false on this page.
externalScriptLoaded = false;
//var externalScriptLoaded = false; //DOES NOT WORK, WHY ????
var counti = 1;//we need a fail safe.

checkScriptLoaded();//call the function.

function checkScriptLoaded() {
if(!externalScriptLoaded) {
alertMe("BooHoo it's NOT loaded");
counti++;//increment counter
if (counti != 10){ //is 5 seconds enough?
setTimeout( checkScriptLoaded, 500 );//run this script every half second
}else{
alertMe("The script failed to load");
}
} else {
// script loaded and ready to use
alertMe("Hoorah it's loaded");
initRunSB();//function to run the script
//return true;
}
}


Again, Thanks to all!
Htizo

Philip M
01-19-2009, 08:16 AM
//var externalScriptLoaded = false; //DOES NOT WORK, WHY ????


Because if the variable externalScriptLoaded is declared within a function in the sb.js file the var prefix makes the variable local to that function only.


A child of five would understand this. Send someone to fetch a child of five. - Groucho Marx

htizo
01-20-2009, 07:04 AM
Thanks Phil.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum