0

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!

3
  • 3
    polltime should have a value when wait <= 10 Commented Jun 15, 2015 at 16:28
  • 1
    It's clear that there is some more code before your if (content == same): you had better include that too, because the last row of your code forces me to think that the problem is related to what is acting before your initial if statement! Commented Jun 15, 2015 at 16:31
  • 1
    Is this in a setInterval() call? A setTimeout()? Where and when is polltime declared and initialized? Commented Jun 15, 2015 at 17:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This does not seem to work. Polltime remains 0 even though I instruct it to update to 6000.
At the time you start the function polltime will be 0, at the time the function exits polltime will be 6000. The thing is, if you want to change a value that you already used at runtaime is juts not possible. This is because when the function was fired it took polltime value in that time which it was, 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.