Reputation: 1
$('form[name=addform]').submit(function() {
if(error == true){
return false;
}else{
$('#loading').empty();
$('#loading').append("<img src = './images/ajax-loader.gif'/>");
$('#loading').show();
setTimeout(function(){
return true;
}, 4000);
}
error = false;
});
i need to load gif image before executes the return true. i do not use ajax for this please help
Upvotes: 0
Views: 385
Reputation: 2787
JavaScript is asynchronous, not synchronous - setTimeout
will not 'block' the execution of the next line of code. (See related What is the difference between synchronous and asynchronous programming (in node.js))
To submit the form after a delay (for what purpose is unclear) you will first need to stop the form submitting (by cancelling the event) then submit the form once again as when setTimeout
callback is executed.
$('form').on('submit', function(event) {
// Prevent the form from submitting
event.preventDefault();
// 'this' is referring to the event target
var form = this;
// It may not be required to wrap the `form.submit` method but it's usually safer to do so
setTimeout(function() {
form.submit()
}, 4000);
});
Here is an example http://jsfiddle.net/xppNp/1/
Upvotes: 0
Reputation: 1116
I'm not exactly sure what you are trying to accomplish, but that code won't work. setTimeout()
does not pause the function it has been called from, nor does the return
statement in the callback affect it.
Upvotes: 3