Listen to js variable change

Let’s say there is a code in place 2

var place2IsReady = true;

In place 1 I need to implement the logic below :

Once place2IsReady value was changed (to true) then display alert('ready!');

Notes:

  • place2IsReady variable is not available in the scope of place 1.
  • the code from place 1 gets executed before place 2 gets executed (or there is a race condition).

Solution 1

I believe I can use window.place2IsReady instead and use setTimeout/setInterval in place 1 until I get window.place2IsReady === true.

Any better options? Using Listeners? On the variable change?

P.S. I need to track only first possible change of place2IsReady.

Is there a better way? Thank you.

120 thoughts on “Listen to js variable change”

  1. You actually make it seem really easy together with your presentation however I
    find this topic to be actually one thing which I believe I’d by no means understand.
    It kind of feels too complicated and extremely wide for me.
    I am looking ahead on your next submit, I will try to get the hang
    of it!

    Reply
  2. You made some really good points there. I checked on the internet for more information about the issue and found most individuals will go along with your views on this website.

    Reply
  3. Howdy are using WordPress for your site platform? I’m new
    to the blog world but I’m trying to get started and create my own. Do you require any coding expertise to make
    your own blog? Any help would be really appreciated!

    Reply
  4. Oh my goodness! Impressive article dude! Many
    thanks, However I am going through problems with your
    RSS. I don’t understand why I cannot subscribe to it.
    Is there anyone else having similar RSS problems?

    Anybody who knows the solution can you kindly respond?
    Thanx!!

    Reply
  5. Hello, Neat post. There is a problem with your site in web explorer, would test this?
    IE still is the market chief and a good portion of folks will omit your fantastic writing due to
    this problem.

    Reply
  6. Thank you for some other informative site. The place else may just I get that
    kind of information written in such an ideal means?
    I have a challenge that I’m simply now running on, and I have been at the look out for such information.

    Reply
  7. Great blog right here! Also your web site lots up very fast!
    What web host are you using? Can I am getting your affiliate hyperlink
    on your host? I wish my website loaded up as quickly as yours lol

    Reply
  8. Great post. I used to be checking continuously this blog and I am inspired!
    Extremely useful info particularly the last phase 🙂 I deal
    with such info much. I was looking for this particular info for
    a long time. Thank you and best of luck.

    Also visit my blog – raycon

    Reply
  9. Hi, i think that i saw you visited my site so i came to
    “return the favor”.I’m attempting to find things to improve my website!I suppose its ok to use a few of your ideas!!

    Reply
  10. Thanks , I’ve recently been searching for info
    approximately this topic for ages and yours is the greatest I have found out
    so far. However, what in regards to the bottom line? Are you positive concerning the supply?

    Reply
  11. I just like the helpful info you supply for
    your articles. I will bookmark your blog and test once more here frequently.
    I am rather certain I will learn plenty of new stuff proper right here!
    Good luck for the following!

    Reply

Leave a Comment