初学面向对象javascript,写程序如下(目的是使一个Div高度慢慢缩短,最后消失):
var animateObj = function() {
this.animateTimer = "";
this.intervalTime = 1;
this.divEl = null;
this.divHeight = 0;
this.shortElRegularly = function() {
if (divHeight > 0) {
divEl.style.height = divHeight.toString() + "px";
divHeight--;
}
else {
clearInterval(animateTimer);
hide(divEl);
divEl.style.removeAttribute("height");
}
}
this.startShortAnimate = function(el) {
divEl = el;
divHeight = divEl.scrollHeight;
alert('1');
animateTimer = setInterval(this.shortElRegularly, intervalTime);
alert('2');
}
}
function cutDisplay(el) {
if (!isHide(el)) {
var animateObjInstance = new animateObj();
animateObjInstance.startShortAnimate(el);
}
}
调用后只能执行到alert('1');执行不知alert('2');
请问setInterval应该怎么写啊?还有哪些地方写的不合适的?请大家赐教。。。
animateTimer = setInterval(this.shortElRegularly, intervalTime);
改成这样试试:
animateTimer = setInterval(function(self){return function(){self.shortElRegularly();}}(this), intervalTime);
不好意思,写漏了一个括号.