代码如下:
首先实现该功能的js对象LoadingMsg:
var Class = {
create: function() {
return function() { this.init.apply(this,arguments); }
}
}
var LoadingMsg = Class.create();
LoadingMsg.prototype = {
init: function(spanId, spanMsg) {
this.intervalID = -10000;
this.spanId = spanId;
this.spanMsg = spanMsg;
this.timespan = 1000;
this.pointNum = 3;
this.initPointMsg = "...";
},
加载: function() {
var maxLength = this.spanMsg.length this.pointNum;
var currentSpanMsg = document.getElementById(this.spanId).innerHTML;
if (currentSpanMsg.length document.getElementById(this.spanId).innerHTML = ".";
}
else {
document.getElementById(this.spanId).innerHTML = this.spanMsg;
}
},
开始: function() {
document.getElementById(this.spanId).innerHTML = this.spanMsg this.initPointMsg;
var callObj = this;
this.intervalID = setInterval(function() { callObj.Loading(); }, this.timespan);
},
结束: function() {
document.getElementById(this.spanId).innerHTML = "";
clearInterval(this.intervalID);
}
}
关键点:
如果把
var callObj = this;
this.intervalID = setInterval(function() { callObj.Loading(); }, this.timespan);
写成:
this .intervalID = setInterval(this.Loading, this.timespan);
在执行Loading方法时发现发现this.spanMsg的错误。
因为在setInterval里的第一个参数里的this是windows对象,而不是LoadingMsg对象。
应用该方法:
>
;
>
>
var loadMsgObj = new LoadingMsg("spanId","loading");
函数 StartLoading() {
loadingMsgObj.Start();
}
函数 EndLoading() {
loadingMsgObj.End();
}
脚本>
身体>
源自prototype.js里经典创建Js对象的
var Class = {
create: function() {
return function() { this.init.apply(this,arguments); }
}
}
var LoadingMsg = Class.create();
在Class.create()的时候做了2件事,其中1个是创建了LoadingMsg的对象,即var LoadingMsg = function() {};
另外一件事就是调用LoadingMsg的init方法,初始化LoadingMsg里的真空变量,相当于c#里的构造函数作用。
如果你觉得这很装逼的话,如果你更喜欢简单朴实的女孩子的话,也可以改写LoadingMsg对象:
var LoadingMsg = function() { };
LoadingMsg.prototype = {
init: function(spanId, spanMsg) {
this.intervalID = -10000;
this.spanId = spanId;
this.spanMsg = spanMsg;
this.timespan = 1000;
this.pointNum = 3;
this.initPointMsg = "...";
},
加载: function() {
var maxLength = this.spanMsg.length this.pointNum;
var currentSpanMsg = document.getElementById(this.spanId).innerHTML;
if (currentSpanMsg.length document.getElementById(this.spanId).innerHTML = ".";
}
else {
document.getElementById(this.spanId).innerHTML = this.spanMsg;
}
},
开始:函数(spanId, spanMsg) {
this.init(spanId, spanMsg);
document.getElementById(this.spanId).innerHTML = this.spanMsg this.initPointMsg;
var callObj = this;
this.intervalID = setInterval(function() { callObj.Loading(); }, this.timespan);
},
结束: function() {
document.getElementById(this.spanId).innerHTML = "";
clearInterval(this.intervalID);
}
}
所不同的是把init的过程调到Start时执行,因此调用的时候就变成了
复制代码
代码如下:
复制代码function StartLoading() { loadingMsgObj.Start("spanId", "loading"); } 从面向对象的习惯上来说,我个人还是偏向第一种写法,在实例化对象的时候即格式化参数,是执行对象方法的时候格式化另外关于setInterval方法传递参数时,如果参数是简单的字符串,可以setInterval("DisplayXYZ('xyz')",1000); 如果参数是对象, 则可以setInterval(function(){DisplayXYZ(obj);},1000); LoadingMsg还是主要用在Ajax成功中,获取执行时间可能达到的场景,在发送请求后loadingMsgObj.Start(),在获取响应时loadingMsgObj.End()。