I have the following code :
function job(job_id){
$( "#demo" ).append("Next" + job_id);
setInterval(job, 1000, 1);
}
job(1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<div id="demo">
</div>
Why the function doesn't wait 1 second before being executed ?
My goal is to use it in a function which contain an ajax call.
function job(job_id){
$.ajax({
type: "POST",
url: "data/job.php",
data: { 'job_id' : job_id },
dataType: 'json',
success: function(data){
$.each(data, function(index, element) {
if(index == 'status'){
if(element == "Terminated"){
$("#cronExecutebtn").prop("disabled",false).html("Sync")
console.log('stop')
clearInterval(myInterval);
}else if(element == "In progress"){
console.log('continu');
job(1)
//const myInterval = setInterval(job(1), 1000);
//const myInterval = setInterval(job, 1000, 1);
//setInterval(job(job_id), 1000);
//const myTimeout = setTimeout(job(job_id), 5000);
}
}
});
}
});
}
const myInterval = setInterval(job, 1000, 1);
The goal is, when element is equal to "In progress", wait 1 second and re run the function to check the status, if the status is equal to "Terminated" then stop.