Javascript函数在等待ajax响应时循环调用
javascript function called on loop while waiting for ajax response
我有两个.asp脚本-第一个复制sys的内容。表和系统。视图到SQL中的另一个表。第二个脚本计算完成的百分比
(count() from newtable/(count() from sys。Tables + count(*) from sys.views)) * 100
我有一个函数来调用第一个脚本。
function importTablesAndViews()
{
xmlhttpImportTablesAndViews=GetXmlHttpObject();
if (xmlhttpImportTablesAndViews==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
url="importtablesandviews.asp";
url=url+"?sid="+Math.random();
xmlhttpImportTablesAndViews.onreadystatechange=function(){if (xmlhttpImportTablesAndViews.readyState==4){
var aspResponse = xmlhttpImportTablesAndViews.responseText;
alert(aspResponse); }}
xmlhttpImportTablesAndViews.open("GET",url,true);
xmlhttpImportTablesAndViews.send(null);
}
我有另一个函数来调用进度脚本
function refreshImportProgress()
{
//alert('Refresh...');
xmlhttpRefreshImportProgress=GetXmlHttpObject();
if (xmlhttpRefreshImportProgress==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
url="importtablesandviewsprogress.asp";
url=url+"?sid="+Math.random();
xmlhttpRefreshImportProgress.onreadystatechange=function(){if (xmlhttpRefreshImportProgress.readyState==4){
var aspResponse = xmlhttpRefreshImportProgress.responseText; document.getElementById('progressDiv').innerHTML = aspResponse; }}
xmlhttpRefreshImportProgress.open("GET",url,true);
xmlhttpRefreshImportProgress.send(null);
}
我想要能够做的是调用refreshImportProgress()函数在3秒的间隔从importTablesAndViews()函数。
这可能吗?我试过谷歌同步ajax请求,但到目前为止还没有太多的运气。
用xmlhttpRefreshImportProgress
的onreadystatechange
处理器中的refreshImportProgress
启动定时器(setTimeout
)(只要进度低于100%):
function refreshImportProgress() {
// ...
xmlhttpRefreshImportProgress.onreadystatechange = function () {
if (xmlhttpRefreshImportProgress.readyState == 4) {
document.getElementById('progressDiv').innerHTML = xmlhttpRefreshImportProgress.responseText;
var progress = parseInt(xmlhttpRefreshImportProgress.responseText, 10);
// ↑ this assumes the value of responseText is a integer only or a integer with a percentage symbol (44%)
if (progress < 100) {
setTimeout(refreshImportProgress, 3000);
}
}
}
// ...
}
在importTablesAndViews()
中,发送请求后调用refreshImportProgress
:
function importTablesAndViews() {
// ...
xmlhttpImportTablesAndViews.send(null);
refreshImportProgress();
}
如果我误解了这个问题,我道歉。但我不确定是否有任何与Ajax有关的3秒计时器是最好的方式。例如,如果你的浏览器非常慢,你就不能保证第一个方法的更改及时完成。另外,对于快速浏览器来说,3秒的等待可能是不必要的。
如果你必须这样做,只需调用setTimeout()http://www.w3schools.com/jsref/met_win_settimeout.asp
似乎更有可能你应该实现一个承诺,当你的问题需要时间解决时调用。https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
甚至是事件侦听器。这是一个清单。http://www.w3schools.com/jsref/dom_obj_event.asp
不应该真的需要使用timeout,除非你只是想延迟异步的事情直到下一个滴答。
- 等待循环调用的所有承诺完成
- 为循环调用JavaScript中的SQL
- 如何在每个元素上循环调用jquery.each的数组
- 而循环调用setTimeout不起作用
- jquery每个带有参数的循环调用函数
- 本机 Ajax 中的循环调用函数
- jQuery函数使用包含setTimeout的每个循环调用自身,立即发生,而不是在结束时发生
- 使用for循环调用setTimeout
- 在jquery get请求中从for循环调用var i
- 使用增量ID从循环调用Javascript
- 用javascript中的数组循环调用函数
- document.ready用angular.js以无限循环调用
- 如何在从循环调用的方法中动态分配参数
- 如何在HTML中从for循环调用jQuery函数
- Javascript函数在等待ajax响应时循环调用
- 使用for循环调用函数时浏览器崩溃
- Javascript for循环中的函数被另一个for循环调用
- Javascript循环调用函数
- 你能从PHP循环调用JavaScript函数吗?
- 而Number原型扩展中的循环调用函数一次,然后出现未定义的错误