I have an Ajax request which I want to launch every 5 seconds to see if a process has finished. For this I am trying to use setTimout(), however it just launches one process after the other without respecting the timeout. I've read multiple questions about this on SO and tried to implement the answers, however I cannot get it to work. What do I have to change to get this to work?
<script type="text/javascript">
$(function() {
$(document).ready(function() {
var user = $('.user-id').text()
var url = '/ajaxurl'
var success = 0
var time = 0
var dataString = 'email='+user;
checkServer(url,user,time,success,dataString);
});
function checkServer(url, user, time, success, dataString) {
setTimeout(
$.ajax({
type: "POST",
url: url,
data: dataString,
success: function(data) {
if (data == 'True') {
success = 1
window.location = "https://www.someurl.com";
}
else {
time++
console.log(time);
console.log(data);
if ((success == 0) && (time < 60)) {
checkServer(url, user, time, success, dataString)
}
}
}
}), 5000);
};
});
</script>