我有这个方法:
$(document).ready(function(){
var rootPath = getRootPartofPathName();
(function poll() {
setTimeout(function () {
$.ajax({
type: 'GET',
url: rootPath + '/data/notifications?method=getNotificationSizes',
success: function (data) {
handleNotifications(data);
},
complete: poll
});
}, 5000);
})();
});
但它只在页面加载后调用5秒钟。如何让它在第一页加载时开始轮询?
答案 0 :(得分:2)
在回调中创建timeout
,也不需要自动执行功能,然后只需在页面加载时调用poll()
:
function poll() {
$.ajax({
type: 'GET',
url: rootPath + '/data/notifications?method=getNotificationSizes',
success: function (data) {
handleNotifications(data);
setTimeout(poll, 5000);
},
complete: function() {
}
});
}
poll();