var loadBar={
count:0,
start:function(obj){
var t=this;
return setInterval(function(){
++t.count;
obj.style.width=t.count+'px';
if(t.count >= 500){
clearInterval(t.start()); //这个清除函数 不对 不知道怎么改了
document.getElementById("msg").innerHTML="加载完成";
}
},10);
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="a" style="width:200px;border:dashed black 1px;">
</div>
<div id="msg1">
here1
</div>
<div id="b" style="width:200px;border:dashed black 1px;">
</div>
<div id="msg2">
here2
</div>
<script>
function LoadBar(obj, msg, time, max) {
this.count = 0;
this.obj = obj;
this.msg = msg;
this.time = time;
this.max = max;
function PolledRequestSender(requesterObj, time) {
this.ref = null;
this.time = time;
this.requesterObj = requesterObj;
}
PolledRequestSender.prototype = {
getIntervalFn : function(requesterObj) {
return function() {
requesterObj.apply(requesterObj);
};
},
start : function() {
this.ref = setInterval(this.getIntervalFn(this.requesterObj), this.time);
},
stop : function() {
clearInterval(this.ref);
}
};
this.mypoller = new PolledRequestSender(this, this.time);
}
LoadBar.prototype = {
apply : function() {
this.count++;
this.obj.style.width = this.count + 'px';
if(this.count >= this.max) {
this.mypoller.stop();
this.msg.innerHTML = "加载完成";
}
},
start : function() {
this.mypoller.start();
}
};
var loadBar = new LoadBar(document.getElementById("a"), document.getElementById("msg1"), 10, 500);
loadBar.start();
var loadBar2 = new LoadBar(document.getElementById("b"), document.getElementById("msg2"), 100, 100);
loadBar2.start();
</script>
</body>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。