作者fillano (冒牌費大公)
看板Ajax
標題Re: [問題] 關於物件與計時器
時間Thu Mar 6 09:48:51 2008
這是scope解析問題。
setTimeout, setInterval是window物件底下的函數,你在裡面用到this
這個this會指到window而不是你想的地方。
你可以先把this傳給一個區域變數,然後在setTimeout或setInterval中用這個變數
例如:
addtext=function(){
this.add=function(){
document.getElementById('test').value+='a';
var thisref = this;
setTimeout(thisref.add(),1000);
}
}
--
Sapere Aude! 這就是啟蒙運動的口號!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.250.95.16
1F:推 awpadam:謝謝您的回應,又學到了。 03/06 18:04