我想从setInterval()调用函数.这是一个想法:
class Greeter {
element: HTMLElement;
span: HTMLElement;
timerToken: number;
constructor(element: HTMLElement) {
this.element = element;
this.element.innerHTML += "The time is: ";
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date().toUTCString();
//this.element.style.cssText = "-webkit-transform:rotate(7deg)";
//this.element.style.transition = "-webkit-transform: rotate(180deg)";
}
start() {
this.timerToken = setInterval(this.runningLoop(this.element), 500);
}
stop() {
clearTimeout(this.timerToken);
}
runningLoop(element: HTMLElement) {
this.element.style.cssText = "-webkit-transform:rotate(7deg)";
}
}
window.onload = () => {
var el = document.getElementById('content');
var greeter = new Greeter(el);
greeter.start();
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到一个例外:
第13行第9列的未处理异常.Microsoft JScript运行时错误:参数无效.
所以我尝试如下:
this.timerToken = setInterval(function () { this.runningLoop(this.element) }.bind, 500);
Run Code Online (Sandbox Code Playgroud)
没有例外但没有任何反应..
有任何想法吗 ?
Ant*_*Chu 19
setInterval(this.runningLoop(this.element), 500);
Run Code Online (Sandbox Code Playgroud)
以上this.runningLoop
在传递给它之前调用setInterval
,setInterval
期待一个函数但是正在接收undefined
.用箭头功能包裹呼叫......
setInterval(() => this.runningLoop(this.element), 500);
Run Code Online (Sandbox Code Playgroud)
既然你没有使用元素参数runningLoop
,你可以删除参数并将方法传递给setInterval
...
setInterval(this.runningLoop, 500);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31810 次 |
最近记录: |