chr*_*leu 5 ajax jquery ajax-polling
$(document).ready(function() {
(function poll() {
setTimeout(function() {
$.ajax({
url: "/project1/api/getAllUsers",
type: "GET",
success: function(data) {
console.log("polling");
},
dataType: "json",
complete: poll,
timeout: 5000
}), 5000
});
})();
});?
Run Code Online (Sandbox Code Playgroud)
这只是保持执行速度与服务器响应速度一样快,但我希望它只会每5秒轮询一次.有什么建议?
编辑:我应该补充,请求完成后5秒钟会更好.
看来你已经设法把你的setTimeout延迟论点写在了错误的地方.
$(document).ready(function() {
(function poll() {
setTimeout(function() {
$.ajax({
url: "/project1/api/getAllUsers",
type: "GET",
success: function(data) {
console.log("polling");
},
dataType: "json",
complete: poll,
timeout: 5000
}) //, 5000 <-- oops.
}, 5000); // <-- should be here instead
})();
});?
Run Code Online (Sandbox Code Playgroud)
如果你按照括号,你会看到你正在调用setTimeout:
setTimeout(function () {
$.ajax(), 5000
})
Run Code Online (Sandbox Code Playgroud)
应该是
setTimeout(function () {
$.ajax();
}, 5000)
Run Code Online (Sandbox Code Playgroud)
这应该在前一个完成后5秒调用AJAX轮询.
| 归档时间: |
|
| 查看次数: |
10852 次 |
| 最近记录: |