1

I am trying to check if a variable is equal to 1 using javascript...

myvalue = 1;

function check() {
    if (myvalue == 1) {
        return setTimeout(check, 1000);
    }

    alert("Value Is Set");
}

check();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I am planning on adding a delay to the setting of the variable but for now why is this simple version not working?

Liam
  • 22,818
  • 25
  • 93
  • 157
fightstarr20
  • 8,600
  • 24
  • 103
  • 203
  • Unclear, what you are asking for? You are calling the same function and its going inside `if` loop everytime. So? – Milan Chheda Aug 14 '17 at 08:45
  • 1
    You would be better off listening for a variable change event of some kind, like in this question: [Listening for variable changes in JavaScript or jQuery](https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript-or-jquery) – Liam Aug 14 '17 at 08:46
  • 1
    You don't show any jQuery code here or any code that ever changes the value of `myvalue` – Lennholm Aug 14 '17 at 08:47
  • 1
    What are you trying to achieve? –  Aug 14 '17 at 08:48
  • in my full code myvalue is set when an image has been uploaded, i am trying to get my code to wait for it to happen – fightstarr20 Aug 14 '17 at 08:49
  • 1
    You can go for asynchronous call then. Why timeout? –  Aug 14 '17 at 08:50
  • Which image uploader are you using? –  Aug 14 '17 at 08:55
  • When an image uploaded, a variable should set to some value.. right? OR something else? –  Aug 14 '17 at 08:57

1 Answers1

1

Using setTimeout(check, 1000); calls the function only once. That's not what you are looking for.

What you're looking for is setInterval which executes a function every n miliseconds.

Look at the below example which waits for the value to be 1, using setInterval, and then clearing the setInterval instance once it does.

Wait 4 seconds when running the snippet below:

// First - set the value to 0
myvalue = 0;

// This variable will hold the setInterval's instance, so we can clear it later on
var interval;

function check() {
    if (myvalue == 1) {
        alert("Value Is Set");

        // We don't need to interval the check function anymore,
        // clearInterval will stop its periodical execution.
        clearInterval(interval);
    }
}

// Create an instance of the check function interval
interval = setInterval(check, 1000);

// Update the value to 1 after 4 seconds
setTimeout(function() { myvalue = 1 }, 4000);
Koby Douek
  • 14,709
  • 15
  • 58
  • 84
  • It does not return "an instance of the setTimeout function". It returns it's return value. The original code should also work, the problem is most likely that the value is never set. – Matthias247 Aug 14 '17 at 08:51
  • It's still wrong. On the first timeout `check` will be called and the next `setTimeout` will be started. Even though `return` keyword is unnecessary the overall behavior is not different to your setInterval based one. – Matthias247 Aug 14 '17 at 08:54
  • Ok, I had bad eyes. The OPs code would have worked if the check would have been `if (myvalue != 1)`, which is what I first saw. Then an synchronous setTimeout loop would have kicked in and behaved similar to your setInterval based solution. I just wanted to point out that setInterval isn't necessary, and setTimeout can do the same thing (if properly used). – Matthias247 Aug 14 '17 at 09:00