Suppose there exists a code snippet at point 2
var point2IsReady = true;
At point 1, I am tasked with implementing the following logic:
Once the value of point2IsReady is changed (to true), then display an alert saying 'ready!'.
Considerations:
- The variable point2IsReady is not accessible in the scope of point 1.
- The code at point 1 executes before the code at point 2 (or there may be a race condition).
Potential Solution 1
I propose using window.point2IsReady instead and employing setTimeout/setInterval at point 1 until the condition window.point2IsReady === true
is met.
Any alternative suggestions? Implementing Listeners or Reacting to Variable Changes?
Note: Only the first occurrence of a change in place2IsReady needs to be tracked.
Can you think of a more optimal approach? Thank you.