I have an anonymous polling function which has a setTimeout to kick off a ajax call every 30 seconds. However, the anonymous function does kick off immediately but for some reason the ajax call does not kick off immediately, but starts only after the 30 seconds. Am I missing something like calling it immediately to trigger right away?
(function poll() {
console.log('polling called');
setTimeout(function () {
$.ajax({
url: "/server/call",
type: 'GET',
dataType: "json",
timeout: 30000,
success: function (data) {
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/" + (currentdate.getMonth() + 1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
console.log(datetime);
console.log('call was successful at: ' + datetime);
}
});
},
30000);
})();
The logging just starts off only after 30 seconds and not right away. Thanks