Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2009
    Thanked 0 Times in 0 Posts

    Passing counter into loop

    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,
    Last edited by htizo; 01-20-2009 at 07:42 AM. Reason: clarify

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    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.
    Last edited by Philip M; 01-20-2009 at 09:20 AM. Reason: Typo


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts