I have the following script:
$(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);
});
});
With the following HTML:
<input type="button" value="button" class="button" />
<div class="message">loading...</div>
For some reason the setTimeout
part is not working. i.e. it does not seem to call the function after 5000ms.
jsFiddle.
You need to replace:
setTimeout("simulate_ajax_call()", 5000);
with:
setTimeout(simulate_ajax_call, 5000);
You should avoid putting ()
at the end of the function name because otherwise it gets called/run immediately :)
You need to drop the quotes and the parenthesis.
Doing setTimeout("simulate_ajax_call()", 5000)
is equivalent of eval()
ing that code, which is automatically running the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With