简体   繁体   English

不断关注价值变化

[英]Constantly watch for value change

I'm trying to constantly watch for change in sessionStorage value and performing a console.log if the sessionValue is not null 我正在尝试不断监视sessionStorage值的变化,如果sessionValue不为null ,则执行console.log

  let intervalId = setInterval(function () {   
     if (sessionStorage.getItem('Data') !== null) {
            console.log('Success');
            clearInterval(intervalId);
        }
    }, 500);

The issue is I feel using a setInterval approach is not that good. 问题是我觉得使用setInterval方法不是很好。 What else can be done instead of using a setInterval . 除了使用setInterval还可以执行其他操作。 Can ES6 proxy implement a watcher. ES6代理可以实现观察者吗?

Polling in JS is a sign of a misunderstanding. 用JS进行轮询是一种误解的迹象。 In JS you want to write your code such that you will be told when something changes, rather than writing code to check if it has changed. 在JS要编写代码,这样当有新的变化,而不是编写代码来检查它是否改变了你会被告知。

In this case, changes to session storage emit a storage event on the window: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent 在这种情况下,对会话存储的更改将在窗口上发出storage事件: https : //developer.mozilla.org/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent

So you can do 所以你可以做

window.addEventListener('storage', function(e) {
  if (e.storageArea === sessionStorage && e.key === 'Data') {
    // Something on another page changed the stored value.
  }
});

You can look at the docs for information about what properties exist on e : https://developer.mozilla.org/en-US/docs/Web/API/StorageEvent 您可以查看文档以获取有关e存在哪些属性的信息: https : //developer.mozilla.org/en-US/docs/Web/API/StorageEvent

Note that this event only fires if another page changes the stored value. 请注意,仅当另一个页面更改了存储的值时,才会触发此事件。 If it is your own application that is making the changes, it would be up to you to write your code such that you can notify other pieces of code when changes happen. 如果是您自己的应用程序进行更改,则由您自己编写代码,以便在发生更改时通知其他代码段。 You could for instance write an object that you'd use everywhere in your own code that you could attach callbacks to, to act as an intermediate. 例如,您可以在自己的代码中编写一个可以在各处使用的对象,并将其附加到回调中,以充当中间对象。

Why not writing a wrapper around sessionStorage access and hook some actions on get/set? 为什么不围绕sessionStorage访问编写包装器并在get / set上挂接一些操作?

 function makeSSPersistence(callback) { return { setItem: function(key, value) { window.sessionStorage.setItem(key, value) callback() }, removeItem: function(key) { window.sessionStorage.removeItem(key) callback() }, getItem: function(key) { return window.sessionStorage.getItem(key) } } } function checkCallback() { if (sessionStorage.getItem('Data') !== null) { console.log('Success') } } var ss = makeSSPersistence(checkCallback) ss.setItem('Test', 12345) ss.setItem('Data', 12312412) ss.removeItem('Data') 

Also you can set listener on window object and listen for storage event which notifies you if some operation was made on the storage. 您还可以在window对象上设置侦听window并侦听storage事件,该事件会通知您是否对存储进行了某些操作。

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

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