View Full Version : Passing counter into loop

01-20-2009, 07:14 AM
G'day All,
How do I pass the counti variable into "loadSB"?
and have it available to the setTimeout() loop?

function loadSB() {
if(!externalScriptLoaded) {
counti++; // ERROR HERE !!!
if (counti != 10){
setTimeout( loadSB, 500 ); //loop back
alert("Failed to load");
} else {
return true;

function checkSBLoaded(){
externalScriptLoaded = false; //we need the variable set
var counti = 1;//we need a fail safe in the checkScriptLoaded() function.
if (loadSB()){; //NOT LOADED SO LOAD IT.
blah blah

Thanking you in anticipation,

Philip M
01-20-2009, 08:41 AM
This is like stirring treacle with a piece of bent tin.

As I see it there is nothing wrong with the following:-

<script type = "text/javascript">

var externalScriptLoaded = false; // global variable
var counti = 1; //we need a fail safe.

checkScriptLoaded();//call the function.

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


and within the code of the loaded script:-

externalScriptLoaded = true; // no var

If you create another variable with the keyword var it will either overwrite the existing variable of that name, or if within a function will create a different but identically-named local variable.

Please study this example:-

<script type = "text/javascript">
var x = 10; // this x is a global variable
var x = 20;
alert (x); // 20
function test() {
var x = 99; // this x is a variable local to the function only
alert (x); // 99
alert (x); // 20

Quizmaster: Three stumps with two bails on top are essential equipment in which sport?
Contestant: Horse racing.