zoukankan      html  css  js  c++  java
  • localstorage 更新监测 storage事件

    1、存储更新监测

    存储状态监测的原理是storage事件。storage事件说明:

    https://developer.mozilla.org/zh-CN/docs/Web/API/StorageEvent

    storage事件是注册在window上的。

    2、示例

    同域下2个文件,分别为test.html和test1.html。

    test.html文件为:

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>storage事件</title>
        </head>
    
        <body>
            <script type="text/javascript">
                setTimeout(function(){
                    window.localStorage.setItem('a', 2)
                },1000)
                window.addEventListener("storage", function(e) {
                    console.log(e)
                });
            </script>
        </body>
    
    </html>

    test1.html文件为:

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>storage事件</title>
        </head>
    
        <body>
            <script type="text/javascript">
                window.localStorage.setItem('a', 1)
                window.addEventListener("storage", function(e) {
                    console.log(e)
                });
            </script>
        </body>
    
    </html>

    运行2个文件,test1.html的控制台输出为:

    即能监测到localStorage的变化。

    3、说明

    (1)是同域的不同文件会监测到存储值的变化。

    (2)同一个文件,存储值的变化,监测不到。如:

    <!DOCTYPE html>
    <html lang="zh">
    
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>storage事件</title>
        </head>
    
        <body>
            <script type="text/javascript">
                window.localStorage.setItem('a', 1)
                setTimeout(function(){
                    window.localStorage.setItem('a', 2)
                },2000)
                setTimeout(function(){
                    window.localStorage.setItem('a', 3)
                },3000)
                setTimeout(function(){
                    window.localStorage.setItem('a', 4)
                },4000)
                window.addEventListener("storage", function(e) {
                    console.log(e)
                });
            </script>
        </body>
    
    </html>

    运行上述文件,控制台没有输出内容。

  • 相关阅读:
    kill all process on a specific port on linux 码农
    rxjs 学习系列二(环境搭建) 码农
    实拍图与摄相头自动合成软件(效果) 码农
    js中for in的用法
    Android sdk资源包res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
    js中addEventListener中第3个参数
    【agc006f】Blackout
    Trie&可持久化Trie
    【agc014d】Black and White Tree
    【agc009b】Tournament
  • 原文地址:https://www.cnblogs.com/mengfangui/p/10736317.html
Copyright © 2011-2022 走看看