js worker 解决移动端定时器锁屏后不继续执行的问题
2016-08-12 14:14
330 查看
一直知道在js里面使用计时器会阻塞ui渲染,并且在移动端锁屏后计时器不会继续执行。网络上常用的思路是启动定时器时记录当前时间,当锁屏进来后通过当前时间和记录时间对比去解决锁屏后不继续执行的问题,个人觉得这种解决办法不优雅。 最近看了关于worker的知识,就想着用worker写是不是能解决这个问题,试了试,还真行,代码附上。
worker.js
function timeout(time) {
var clock=setInterval(function() {
time=time*1 - 1;
if (time != 0) {
postMessage(time);
}else{
clearInterval(clock);
}
}, 1000);
}
this.addEventListener("message", function(e){
timeout(e.data);
});
页面
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>1</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
<meta content="telephone=no" name="format-detection">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<!-- <link rel="stylesheet" type="text/css" href="../../css/base/shoppingBag.css"> -->
</head>
<body>
<div id="count" style="font-size: 18px;color: red;height: 35px;line-height: 35px;width: 100%;text-align: center;">60</div>
<div id="button" style="height: 35px;line-height:35px;text-align: center;padding: 10px 20px;background-color: green;">按钮</div>
</body>
</html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
var myworker= new Worker("work.js");
$('#button').on('click',function(e){
myworker.postMessage(60);
});
myworker.addEventListener("message", function(e){
$('#count').html(e.data)
});
});
</script>
worker.js
function timeout(time) {
var clock=setInterval(function() {
time=time*1 - 1;
if (time != 0) {
postMessage(time);
}else{
clearInterval(clock);
}
}, 1000);
}
this.addEventListener("message", function(e){
timeout(e.data);
});
页面
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>1</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
<meta content="telephone=no" name="format-detection">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<!-- <link rel="stylesheet" type="text/css" href="../../css/base/shoppingBag.css"> -->
</head>
<body>
<div id="count" style="font-size: 18px;color: red;height: 35px;line-height: 35px;width: 100%;text-align: center;">60</div>
<div id="button" style="height: 35px;line-height:35px;text-align: center;padding: 10px 20px;background-color: green;">按钮</div>
</body>
</html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
var myworker= new Worker("work.js");
$('#button').on('click',function(e){
myworker.postMessage(60);
});
myworker.addEventListener("message", function(e){
$('#count').html(e.data)
});
});
</script>
相关文章推荐
- Spring的Quartz定时器同一时刻重复执行二次的问题解决
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- spring 定时器 @Component 执行两次 问题解决
- VS2010 "缺少dlmgr.dll 无法继续执行代码。重新安装程序可能会解决此问题"解决方案
- 解决关于如何实现锁屏后继续播放音乐的问题
- Spring的Quartz定时器同一时刻重复执行二次的问题解决
- Spring的quartz定时器重复执行二次的问题解决
- keil中调试时执行到prinitf停住不继续进行问题解决
- Spring的定时器出现执行两次的问题剖析及解决
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- 解决关于如何实现锁屏后继续播放音乐的问题
- 解决关于如何实现锁屏后继续播放音乐的问题
- tomcat启动timer定时器会执行两次问题解决方案
- js 通过调用方法终止js代码继续执行的问题(采用throw解决)
- Spring的Quartz定时器同一时刻重复执行二次的问题解决
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- Spring的quartz定时器同一时刻重复执行二次的问题解决
- 解决关于如何实现锁屏后继续播放音乐的问题
- Spring的quartz定时器重复执行二次的问题解决