【发布时间】:2011-09-07 15:22:41
【问题描述】:
我正在执行 AJAX 调用(常规 JS),如果它需要超过 500 毫秒,我想打开我的“请稍候”框。
一般情况下,如果我想立即把密码箱放上去,我会这样做:
// show semi-transparent grey screen to block access to everything underneath
divGreyCoverAllNode.style.display = 'inline';
// show PW box. Prior to these lines, both coverall and PW were display=none
divPleaseWaitNode.style.display = 'inline';
// now do the AJAX and follow-up inside a zero timer; the timer is necessary to
// make the system pause to display the screen changes we previously invoked
setTimeout( function() {
// do my ajax call here, then after the call...
// take down the PW stuff
divPleaseWaitNode.style.display = 'none';
divGreyCoverAllNode.style.display = 'none';
},
0
);
就像我上面所说的,我想做的是仅当 AJAX 没有在 500 毫秒内完成时才显示 PW。理想情况下是这样的:
// set a timer to display PW in 500 milliseconds
myTimeEvent = setTimeout( function() {
divGreyCoverAllNode.style.display = 'inline';
divPleaseWaitNode.style.display = 'inline';
},
500
);
// do my ajax call here, then after the call...
clearTimeout(myTimeEvent);
// take down the PW stuff, in case it was displayed
divPleaseWaitNode.style.display = 'none';
divGreyCoverAllNode.style.display = 'none';
但是当 AJAX 占用时间时,我似乎无法让系统暂停并显示 PW。我已经尝试在零计时器中围绕 AJAX 和后续块,但没有处理。
有什么建议吗?
编辑: 重要事实:这不是异步 ajax 调用。这是一种不寻常的情况,需要一切都等待 ajax 结果。
【问题讨论】:
-
你把
clearTimeout放在ajax调用的响应部分了吗?你的问题不清楚 -
clearTimeout在我得到 AJAX 调用的结果后立即出现。它不是onreadystatechange函数的一部分。但这是一个相当奇怪的 ajax 调用,因为它不是异步的。
标签: javascript html settimeout