日期:2014-05-16  浏览次数:20375 次

一个比较基础的问题
function Divmove(id){
this.div=document.getElementById(id);
this.x=0;
this.y=0;
//我在这里想用window.setInterval定时调用move1,该怎么写啊
}
Divmove.prototype.move1=function(){
this.x=4;
this.y=4;
this.div.style.left=parseInt(this.div.style.left)+this.x+"px";
this.div.style.top=parseInt(this.div.style.top)+this.y+"px";
//想在这里用window.setInterval调用其他的方法活window.setTimeout掉用自身,该怎么写啊 this指来指去的搞不懂啊
}
万分感谢

------解决方案--------------------
重载了一下window.setTimeout,用apply去回调前面的function.以下是测试代码


<script type="text/javascript">
var _st = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
if(typeof fRef == 'function'){
var argu = Array.prototype.slice.call(arguments,2);
var f = (function(){ fRef.apply(null, argu); });
return _st(f, mDelay);
}
return _st(fRef,mDelay);
}

function test(x){
alert(x);
}
window.setTimeout(test,1000,'fason');
</script>
------解决方案--------------------
要新建一个实例才能调用它的方法


比如 var instance1 = new Test();

setInterval(instance1.add,'1000')

要么方法就这样写Test.add = function(){}
setInterval(Test.add,1000)