0
<script>
  let y = 0
  let x = document.querySelector("p")
  setInterval(function(){y++; x.innerHTML = y},1000)
  x.addEventListener("change",change)
  function change(){console.log("i have changed")}

</script>

the script above is kind of what i'm looking for, this has worked for a input element, but i want a this kind of solution, however i am aware it might not be possible, thank you.

  • Can you describe the usage situation? – Ian Mar 04 '22 at 07:56
  • does this helps: https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript – Bjop Mar 04 '22 at 07:58
  • It's possible for properties, but not for variables. For variables there is the [observer design pattern](https://en.m.wikipedia.org/wiki/Observer_pattern) and the implementation [RxJS](https://rxjs.dev/). – jabaa Mar 04 '22 at 08:13

0 Answers0