Javascript 代理模式模拟一个文件同步功能
2024-10-19 07:37:14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
<input type="checkbox" id="5">
<input type="checkbox" id="6">
<input type="checkbox" id="7">
<input type="checkbox" id="8">
<input type="checkbox" id="9">
<input type="checkbox" id="10">
<p id="status"></p>
<script>
var checkbox = document.getElementsByTagName('input');
//本体类,同步逻辑处理
var syncFile = function(id){
//Ajax等同步代码...
document.getElementById('status').innerHTML = '正在同步文件'+id;
}
//代理类,通过一个闭包结构保存cache和timer
var syncFileProxy = (function(){
var cache = [];
var timer = null;
return function(id){
cache.push(id);
//小于2秒的同步请求,则不会处理
if(timer){
return;
}
timer = setTimeout(function(){
syncFile(cache.join(','));
clearTimeout(timer); //清除定时器
timer = null; //重置变量
cache = []; //重置cache数组
},2000);
}
})();
for(var i = 0; i < checkbox.length; i++){
checkbox[i].onclick = function(){
if(this.checked === true){
syncFileProxy(this.id);
//可以随时替换成 syncFile(this.id)
}
}
}
</script>
</body>
</html>
最新文章
- Microsoft Azure News(6) Azure新F系列虚拟机
- Linux下EclipseCDT工程和TFS的持续集成CI实践
- JAVA面试题之实现字符串的倒序输出
- Win7+VMware Workstation环境下的CentOS-Linux网络连接设置
- poj 1330 Nearest Common Ancestors LCA
- Java再学习——sleep(), wait(), notify(), notifyAll()
- iOS开发中EXC_BAD_ACCESS的另类原因
- Js正则表达式提取图片地址
- 何使用CSS写出一个下拉菜单。
- hive udaf 用maven打包运行create temporary function 时报错
- 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能
- JS性能优化之怎么加载JS文件
- ROS_Kinetic_28 turtlebot gazebo demo例子
- Java:接口和抽象类,傻傻分不清楚?
- SpringBoot之配置
- U3D一些使用
- 非常干货之Python资源大全
- Flask学习【第5篇】:用Falsk实现的分页
- 呼叫WCF Service的方法出现Method not allowed异常
- [git hooks] pre-commit 配置
热门文章
- npm 私有模块的管理使用
- XStream将java对象转换为xml时,对象字段中的下划线“_”,转换后变成了两个的解决办法
- 旺财速啃H5框架之Bootstrap(一)
- UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?
- 免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
- 内存映射文件MemoryMappedFile使用
- .NET设计模式访问者模式
- 关于MJRefresh的下拉加载数据bug
- SVN的使用
- SQL数据类型