I have an ajax call wrapped into a function in order to do polling which is
function doPoll(){
alert('GO POLL')
setTimeout(function(){
$.ajax({
'method':'POST',
'url':'/ajax/request_news',
'beforeSend':function(){
alert('Before send')
date = new Date()
var timestamp_last_check = Math.floor(date.getTime()/1000)
},
'data':{'last_check':timestamp_last_check},
'success':function(ret){
ret = $.parseJSON(ret)
},
'complete':function(){
doPoll()
}
})
},5000)
};
This request doesn't work because it says that the var timestamp_last_check is not initialized. I though that putting this in beforeSend
would work but it seems that the data context is retrieved before the beforeSend
call.
I can't put my timestamp initialization outside the AJAX call because there's the setTimeout
who will cause a 5 seconds delay between the initialization of the timestamp and the AJAX call.
I saw that there's a $.ajaxSetup
function but the doc recommend not to use it.
What's the best practice to initialize this timestamp_last_check
var ?
Thanks !