继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

web存储、应用缓存、web workers

极客教程
关注TA
已关注
手记 58
粉丝 286
获赞 3794
客户端存储数据
  1. 两种方式:
    • localStorage-没有时间限制的数据存储
    • 存储特点: localStorage方法存储的数据没有时间限制。第二天,第二周或下一年之后,数据依然可用。
    • sessionStorage-针对一个session的数据存储
    • 存储特点: sessionStorage方法针对一个session进行数据存储,用户关闭浏览器后,数据会被删除
  2. 与cookie做对比:
    之前,这些都是由cookie完成的。但是cookie不适合大量数据的存储,因为它们由每个对服务器的请求来传递,这使得cookie速度很慢而且效率也不高

    代码示例

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <textarea name="" id="ta" cols="30" rows="10"></textarea>
    <button id="btn">save</button>
    <script>
        var ta=document.querySelector('#ta'),
            btn=document.querySelector('#btn');
        if(localStorage.text){
            ta.value=localStorage.text;
        }
        btn.onclick=function(){
            localStorage.text=ta.value;
        }
    </script>
    </body>
    </html>
    应用缓存
  3. 什么是应用程序缓存:
    HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网连接时进行访问
  4. 应用缓存的优势:
    • 离线浏览-用户可在应用离线时使用它们
    • 速度-已缓存资源加载得更快
    • 减少服务器负载-浏览器将只从服务器下载更新过或更改过的资源
  5. 实现缓存:
    如需应用程序缓存,请在文档的<html>标签中包含manifest属性
    manifest文件的建议的文件扩展名是:".appcache"
  6. Manifest 文件:
    • CACHE MANIFEST-在此标题下列出的文件将在首次下载后进行缓存
    • NETWORK-在此标题下列出的文件需要与服务器的连接,且不会被缓存
    • FALLBACK-在此标题下列出的文件规定当页面无法访问时的回退页面(比如404页面)

      代码示例:

      创建文件为:index.html

      <!DOCTYPE html>
      <html manifest="index.appcache">
      <head lang="en">
      <meta charset="UTF-8">
      <title></title>
      <link href="style.css" rel="stylesheet" type="text/css">
      </head>
      <body>
      <h1 class="h1">Hello HTML5!</h1>
      </body>
      </html>

      接下来创建文件为:index.appcache

      
      CACHE MANIFEST

CACHE:
index.html
index.js

NETWORK:
style.css

用chrome打开index.html后检查元素就会发现index.html index.js缓存下来了,而style.css没有缓存。

## web workers
1. 什么是Web Worker?
  web worker是运行在后台的JavaScript,独立于其他脚本,不会影响页面的性能
2. 方法:
  postMessage()-它用于向HTML页面传回一段消息
  terminate()-终止web worker,并释放浏览器/计算机资源
3. 事件:
  onmessage
#### 代码示例
创建主文件:index.html
```html
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="index.js"></script>
</head>
<body>
    <div id="numDiv">0</div>
    <button id="start">start</button>
    <button id="stop">stop</button>
</body>
</html>

创建:index.js

var numDiv;
var work = null;

window.onload = function(){
    numDiv = document.getElementById("numDiv");

    document.getElementById("start").onclick = startWorker;
    document.getElementById("stop").onclick = function(){
        if(work){
            work.terminate();
            work = null;
        }
    }
}

function startWorker(){
    if(work){
        return;
    }
    work = new Worker("count.js");
    work.onmessage = function(e){
        numDiv.innerHTML = e.data;
    }
}

最后创建:count.js

var countNum = 0;
function count(){
    postMessage(countNum);
    countNum++;
    setTimeout(count,1000);
}

count();

作者:cllgeek

github:cll

打开App,阅读手记
10人推荐
发表评论
随时随地看视频慕课网APP

热门评论

localStorage可以实现限时存储的;

manifest离线存储,坑太多了,现在用的不多了吧

index.appcahe :
CACHE MANIFEST
CACHE:
index.html
index.js
NETWORK:
style.css

与index.html对应的嘛?有点不理解

查看全部评论