0

when we're unable to listen to events on the page but need to run a code immedietaly after an if else condition turns true on the webpage. What can we do to run that bunch of code except setInterval method?

setInterval is okay but it'll run the code in every 1000ms or the ms i put in. But i don't want it to run all the time, just one time when the if/else situation turns true. And without listening to events of course. Is there a better practice than setInterval to do this?

Clarity
  • 15
  • 2
  • Several Qs ... 1)Why can't the state change (false to true) not be published? 2) In case it can not be published/dispatched is this state "glued" to some (globally) accessible namespace, and if so, why? 3)How about wrapping a promise around the state change, especially if it is just a one-time change? – Peter Seliger May 29 '22 at 18:50
  • 1
    Would be good to provide any concrete code but as one mentioned few years ago you can modify `set` statments of the variable with adding your code inside. Here is the link https://stackoverflow.com/a/30349801/18891587 – Jaood_xD May 29 '22 at 20:00
  • "*we're unable to listen to events on the page*" - why?! – Bergi May 29 '22 at 21:45
  • 1
    An "`if`/`else` condition" is a *statement*. It is executed only once. It doesn't "turn true". Only state does change, and it should emit an event (or call callbacks etc.) when doing so, so that other code can react to the change and re-evaluate conditions. There's no way around an event listener. – Bergi May 29 '22 at 21:46
  • @Clarity ... as with Bergi's words ... _"There's no way around an event listener."_ ... listening to e.g. even a [custom event](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#example). Alternatively one can choose the path of [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy) and/or [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) based approaches. – Peter Seliger May 29 '22 at 22:25

0 Answers0