我正在尝试使用setTimeout编写一个简单的代码,并且setTimeout不会等待它所设想的时间并且代码立即执行.我究竟做错了什么?
setTimeout(testfunction(), 2000);
Run Code Online (Sandbox Code Playgroud) 简单的说...
为什么
setTimeout('playNote('+currentaudio.id+', '+noteTime+')', delay);
Run Code Online (Sandbox Code Playgroud)
完美地工作,在指定的延迟后调用函数,但是
setTimeout(playNote(currentaudio.id,noteTime), delay);
Run Code Online (Sandbox Code Playgroud)
同时调用函数playNote?
(这些setTimeout()s在for循环中)
或者,如果我的解释太难阅读,这两个函数之间有什么区别?
这是我的问题.我有这个功能来测试代理服务器.
function crawl() {
var oldstatus = document.getElementById('status').innerHTML;
document.getElementById('status').innerHTML = oldstatus + "Crawler Started...<br />";
var url = document.getElementById('url').value;
var proxys = document.getElementById('proxys').value.replace(/\n/g,',');
var proxys = proxys.split(",");
for (proxy in proxys) {
var proxytimeout = proxy*10000;
setTimeout(doRequest(url,proxys[proxy]), proxytimeout);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望以大约10秒的间隔调用'doRequest()'函数,但即使使用setTimeout(),也会立即调用函数.
欢迎任何想法,谢谢.
PS:即使我为'proxytimout'设置了一个任意值,它也没有效果.
我想让一个元素淡入,并在它消失之前在页面上停留7秒.我可以随时取消它.我定义了以下功能.但是当我打电话时info_timeout.setup(ele, 'some msg here'),电子元件刚刚消失并立即消失.
我错过了什么吗?
var info_timeout = {
show_info: function(){
this.ele.html(this.msg).fadeIn('normal');
this.id = setTimeout(this.hide_info(), 7000);
},
hide_info: function(){
console.log(this.ele, this.id);
this.ele.fadeOut('slow');
delete this.id;
},
setup: function(ele, msg) {
this.ele = ele;
this.msg = msg;
this.cancel();
this.show_info();
},
cancel: function() {
if(typeof this.timeoutID == "number") {
clearTimeout(this.id);
delete this.id;
}
}
Run Code Online (Sandbox Code Playgroud)
};
谢谢.