尝试
setTimeout(function () {
var timestamp_last_check = 0; //declare here to make it available to data:
$.ajax({
'method': 'POST',
'url': '/ajax/request_news',
'beforeSend': function () {
alert('Before send')
date = new Date()
timestamp_last_check = Math.floor(date.getTime() / 1000); //set Value here
},
问题强>
var timestamp_last_check仅限于beforeSend:,因此无法在该区域外访问。
'beforeSend': function () {
alert('Before send')
date = new Date()
var timestamp_last_check = Math.floor(date.getTime() / 1000)
},
在OP的评论
之后更新
function doPoll() {
alert('GO POLL');
setTimeout(function () {
var date = new Date();
var timestamp_last_check = Math.floor(date.getTime() / 1000);//set value here
$.ajax({
'method': 'POST',
'url': '/ajax/request_news',
'data': {
'last_check': timestamp_last_check
},
'success': function (ret) {
ret = $.parseJSON(ret)
},
'complete': function () {
doPoll()
}
})
}, 5000)
};
JavaScript异步处理与Ajax请求
博客探讨了JavaScript中setTimeout的使用以及如何在Ajax请求中设置时间戳。在beforeSend回调中定义的timestamp_last_check变量无法在函数外部访问,但通过在doPoll函数中重新设置,实现了在Ajax请求中传递该变量。文章还展示了如何定期执行Ajax请求以检查更新。
451

被折叠的 条评论
为什么被折叠?



