binnathon
binnathon

Reputation: 121

Using JQuery variable inside JQuery script instead of numbers

So I have this script.

     if (content == same){
              wait=wait+1;
     }

     if (wait > 10){
             polltime = 6000;
     }

     if (content != same){
             $('#mydiv').load('content.php');
     }
}, 2000);

This works fine and there isnt any issue. However, if I try change the number 2000 to "polltime" like this

    if (content == same){
            wait=wait+1;
    }

    if (wait > 10){
            polltime = 6000;
    }

    if (content != same){
            $('#mydiv').load('content.php');
    }
}, polltime);

the script stops running. How do I utilize the variable like this? Sorry for this question, I am new to JQuery!

Upvotes: 0

Views: 52

Answers (1)

Solrac
Solrac

Reputation: 931

Your var is NOT in scope anymore. Try to define your variable BEFORE you use the function so you can keep it in scope:

var polltime = 0;

function my_function(){
 if (content == same){
          wait=wait+1;
 }

 if (wait > 10){
         polltime = 6000;
 }

 if (content != same){
         $('#mydiv').load('content.php');
 }
}, polltime);

So the function will execute and replace the value of "polltime". At the end, if wait>10 "polltime" will have a value of 6000.

Upvotes: 1

Related Questions