web存储、应用缓存、web workers

客户端存储数据

  1. 两种方式:
  • localStorage-没有时间限制的数据存储
    • 存储特点: localStorage方法存储的数据没有时间限制。第二天,第二周或下一年之后,数据依然可用。
  • sessionStorage-针对一个session的数据存储
    • 存储特点: sessionStorage方法针对一个session进行数据存储,用户关闭浏览器后,数据会被删除
  1. 与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>

应用缓存

  1. 什么是应用程序缓存:
    HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网连接时进行访问
  2. 应用缓存的优势:
  • 离线浏览-用户可在应用离线时使用它们
  • 速度-已缓存资源加载得更快
  • 减少服务器负载-浏览器将只从服务器下载更新过或更改过的资源
  1. 实现缓存:
    如需应用程序缓存,请在文档的<html>标签中包含manifest属性
    manifest文件的建议的文件扩展名是:".appcache"
  2. 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

<!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

转载于:https://my.oschina.net/cllgeek/blog/1584760

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值