我想在一些细微的时间后显示警报消息为'你想继续吗?' 如果用户说是,那么ajax调用必须在后台运行,否则取消ajax调用。 那么请告诉我如何在ajax调用响应中放置时间条件?如何在微粒时间后在后台运行ajax调用?
2
A
回答
0
window.setTimeout(function() {
if (confirm('Do you want continue?')) {
// Do some ajax?
} else {
// Do nothing!
}
}, 5000);
0
在技术方面它被称为长轮询请求..它是这样的
function longPolling()
{
$.ajax({
//..... your ajax configurations like url, type, dataType...etc
success: function(data){
// your code for handle success response
setTimeout(function(){
longPolling();
},5000) // call again after 5 seconds
},
error: function(xhr){
// your code for handle error response
setTimeout(function(){
longPolling();
},5000) // call again after 5 seconds
}
};
});
longPolling();// call where ever you need
我希望这对您有所帮助
0
使用的setTimeout()函数在javascript或jquery中
0
var time = 5000;
setTimeout(function() {
if (confirm('Do you want continue?')) {
var root = 'http://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts/1',
method: 'GET'
}).then(function(data) {
console.log(data);
});
} else {
// Do nothing!
}
}, time)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
0
我从你的问题理解什么是你问用户之前“您是否愿意继续或不”正确的Ajax调用将进行?
我相信你想确保用户想等待,如果ajax调用需要很长时间?
如果这是你只是设置一个标志的用户按“无”的情况下,只是中止Ajax调用
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax("example.php")
.done(function() {
alert("success");
})
.fail(function() {
alert("error");
})
});
//when user pressed no call the below function if the ajax is not completed
jqxhr.abort()
相关问题
- 1. 如何在另一个Ajax调用之后运行ajax调用?
- 2. 如何在后台运行
- 3. 如何在后台运行其他功能时在后台运行功能?
- 4. 如何在后台运行计时器
- 5. 如何在后台运行计时器?
- 6. 如何显示动画微调,而后台任务使用AJAX
- 7. 运行在后台
- 8. 运行在后台
- 9. 在F#中如何在后台运行阻塞调用?
- 10. 如何在关闭JFrame时在后台运行应用程序?
- 11. 在后台运行并行
- 12. 如何让脚本在后台运行
- 13. 如何在后台运行javascript?
- 14. 如何让方法在后台运行
- 15. 如何在后台运行动画?
- 16. 如何在后台运行程序
- 17. Python:如何在后台运行def
- 18. 如何让活动在后台运行?
- 19. 如何在后台运行QProcess?
- 20. 如何在后台运行活动?
- 21. 如何在后台自动运行?
- 22. 如何在后台运行此程序
- 23. 服务如何在后台运行 - Android
- 24. 如何在后台运行硒webdriver?
- 25. 如何在iPhone的后台运行sqlite?
- 26. ServiceStack:如何使InMemoryTransientMessageService在后台运行
- 27. 如何在后台运行进程? c#
- 28. 如何让Cloud9 IDE在后台运行?
- 29. 如何在后台运行查询
- 30. 如何在后台连续运行ajax请求?
AJAX调用通常在后台运行 – madalinivascu