我正在尝试使用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'设置了一个任意值,它也没有效果.
请考虑以下示例:
<script type="text/javascript">
function alertBox(){
alert('Hello World!');
}
function doSomething(){
setInterval(alertBox(), 5000); //This is for generic purposes only
};
function myFunction(){
setTimeout(doSomething(),3000);
};
myFunction();
</script>
Run Code Online (Sandbox Code Playgroud)
是什么导致它立即执行,而不是等待3秒设置,以及仅执行警报ONCE,而不是按预定的5秒间隔执行?
感谢您的任何帮助,您可以提供!
石匠
我刚刚进入Java.我正在编写一个简单的脚本来打开一个窗口,然后在短暂的延迟后关闭它.我尝试了以下各种内涵,但无济于事.该功能有效(因为它打开,然后关闭窗口),但延迟不会发生.
function manualWindow(){
testWindow = window.open("popup.php","interaction","resizable=0,width=800,height=600,status=0");
setTimeout(testWindow.close(),5000);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
当调试器在d中触发时,下面的代码有一个新的callstack(这里是 jsfiddle )
function c() {
setTimeout( d, 1000 );
}
function d() {
debugger;
}
c();
Run Code Online (Sandbox Code Playgroud)
如果我们修改使用setTimeout( d(), 1000 );哪个代码括号(括号:)
function c() {
setTimeout( d(), 1000 );
}
function d() {
debugger;
}
c();
Run Code Online (Sandbox Code Playgroud)
那么callstack有c()和d()(这里是 jsfiddle ).为什么?
我有两个功能如下,
func1 = function(){
console.log("func1 is called");
}
func2 = function(){
console.log("func2 is called");
setTimeout(func1(),10000)
}
Run Code Online (Sandbox Code Playgroud)
当我拨打电话时func2().我得到的输出但不是预期的输出.你可以看到我使用了一个setTimeout()in func2,我希望在func1执行之前有一些指定的延迟.
但是没有观察到延迟线同时打印到控制台.我在这里做错了什么,或者我错过了什么?请帮忙..
我想让一个元素淡入,并在它消失之前在页面上停留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)
};
谢谢.
<html>
<head>
<script src="jquery-1.9.0.min.js" type="text/javascript"></script>
</head>
<body>
<input id="input" type="text"/>
<script>
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 5000; //time in ms, 5 second for example
//on keyup, start the countdown
$('#input').keyup(function(){
typingTimer = setTimeout(doneTyping(), doneTypingInterval);
});
//on keydown, clear the countdown
$('#input').keydown(function(){
clearTimeout(typingTimer);
});
//user is "finished typing," do something
function doneTyping () {
alert("123") // I get this alert immidiatelly when typing, but not after 5 sec dalay
}
</script>
</body>
Run Code Online (Sandbox Code Playgroud) 我正在使用Chrome扩展程序来获取推文,我想我可以使用setInterval()函数让脚本每分钟运行一次.首先,我尝试给它这样的功能:
setInterval(myFunction(), interval);
Run Code Online (Sandbox Code Playgroud)
但它只会执行一次我的脚本.然后出于好奇我尝试在setInterval()函数中声明函数,如下所示:
setInterval(function() {body of my function}, interval);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但不是一个非常漂亮的解决方案,任何其他任何人这样做或我只是要处理它?
javascript ×10
settimeout ×7
callback ×1
callstack ×1
clear ×1
for-loop ×1
function ×1
loops ×1
setinterval ×1
timeout ×1