我有以下脚本:
$(function() {
$(".message").hide();
function simulate_ajax_call()
{
$.ajax({
url: "/echo/json/",
success: function(){
alert("done");
$(".message").empty().html("done");
$(".message").delay(1000).fadeOut(500);
}
});
}
$('.button').click(function() {
$(".message").fadeIn(500);
setTimeout("simulate_ajax_call()", 5000);
});
});
使用以下 HTML:
<input type="button" value="button" class="button" />
<div class="message">loading...</div>
由于某些原因,setTimeout
部分无法正常工作。即它似乎不会在 5000 毫秒后调用该函数。
jsFiddle .
最佳答案
你需要替换:
setTimeout("simulate_ajax_call()", 5000);
与:
setTimeout(simulate_ajax_call, 5000);
Check out the working example
您应该避免将 ()
放在函数名称的末尾,否则 it gets called/run immediately :)
关于javascript - 加载消息和设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983233/