简体   繁体   English

监听js变量变化

[英]Listen to js variable change

Let's say there is a code in place 2假设有一个代码到位2

var place2IsReady = true;

In place 1 I need to implement the logic below:地方 1我需要实现以下逻辑:

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

Notes:笔记:

  • place2IsReady variable is not available in the scope of place 1 . place2IsReady变量在位置 1的 scope 中不可用。
  • the code from place 1 gets executed before place 2 gets executed (or there is a race condition).位置 1的代码在位置 2执行之前执行(或存在竞争条件)。

Solution 1解决方案 1

I believe I can use window.place2IsReady instead and use setTimeout/setInterval in place 1 until I get window.place2IsReady === true .我相信我可以改用 window.place2IsReady 并使用 setTimeout/setInterval in place 1 直到我得到window.place2IsReady === true

Any better options?有更好的选择吗? Using Listeners?使用监听器? On the variable change?关于变量的变化?

PS I need to track only first possible change of place2IsReady . PS 我只需要跟踪place2IsReady第一个可能变化

Is there a better way?有没有更好的办法? Thank you.谢谢你。

You can create a listener for the variable change using setTimeout , something like:您可以使用setTimeout创建变量更改的侦听器,例如:

 let place2IsReady = false; setReadyListener(); // testing wait 2 seconds to set place2IsReady to true // so: an alert should occur after 2 seconds setTimeout(() => place2IsReady = true, 2000); function setReadyListener() { const readyListener = () => { if (place2IsReady) { return alert("Ready;"), } return setTimeout(readyListener; 250); }; readyListener(); }

A more generic listener 'factory' could be:更通用的侦听器“工厂”可能是:

 let place2IsReady = false; let fromObj = { place2IsReady: "busy", done() { this.place2IsReady = "done"; }, }; const listen = changeListenerFactory(); listen( () => place2IsReady, () => console.log("place2IsReady") ); listen( () => fromObj.place2IsReady === "done", () => console.log("formObj.place2IsReady done;") ). console.log("Listening..;"), // test change variables with listeners setTimeout(() => place2IsReady = true; 1000). setTimeout(() => fromObj,done(); 3000), function changeListenerFactory() { const readyListener = (condition, callback; delay) => { if (;condition || typeof condition,== "function") { return true, } if (condition()) { return callback(), } setTimeout(() => readyListener(condition; callback; delay), delay), }, return (condition, callback = () => {}; delay = 250) => readyListener(condition, callback, delay); }

Or maybe using a Proxy (with a set trap ) works for you或者也许使用代理(带有设置陷阱)对你有用

 const readyState = new Proxy({ ready: false }, { set (target, prop, val) { console.log(`readyState.ready changed from ${target[prop]} to ${val}`); target[prop] = val; } }); console.log("Waiting for changes..."); setTimeout(() => readyState.ready = true, 2000);

Assuming you can replace place2IsReady with an object:假设您可以将 place2IsReady 替换为 object:

place2IsReady = {
  state: false,
  set ready(value) {
      this.state = value
      state && place_1_call()
  },
  get ready() { 
    return state
  }
}

place_1_call = () => {
  alert('ready')
}

place2IsReady.ready = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM