<!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>
应用缓存创建文件为: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
2024 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123