I have an Ajax for which I want to submit, but after been submitted to delay it with 4 seconds. (because Ii have PHP files that need time to calculate).
It will be perfect if you tell me how to put loading bar and where exactly. I guess its have to be after function (msg) .
I try to delay setTimeout(cbutton, 4000); but nothing happens ..
var barray = [];
function cbutton() {
$('input:radio[name="cheking"]:checked').val();
var varies = $("#fromdate").val();
var varies2 = $("#todate").val();
if (varies == "" || varies2 == "") {
alert("Please fill in all 2 fields first and then submit again");
} else {
barray.push(varies + "~" + varies2);
$.ajax({
type: "POST",
url: $('input:radio[name="cheking"]:checked').val(),
data: {
fromdate: varies,
todate: varies2
}
}).done(function(msg) {
setTimeout(cbutton, 4000);
});
}
}
You are calling recursion here, cbutton is calling itself when ajax returns success.
You need something like this
var barray = [];
function cbutton() {
$('input:radio[name="cheking"]:checked').val();
var varies = $("#fromdate").val();
var varies2 = $("#todate").val();
if (varies == "" || varies2 == "") {
alert("Please fill in all 2 fields first and then submit again");
} else {
barray.push(varies + "~" + varies2);
$.ajax({
type: "POST",
url: $('input:radio[name="cheking"]:checked').val(),
data: { fromdate: varies, todate: varies2 }
}).done(function (msg) {
setTimeout(clearForm(), 4000);
});
}
}
clearForm = () => {
$('form.input').forEach(i, input) => { $(input).val('') });
}