Ajax延迟模拟
我有一个小的 ajax jquery 脚本,它为我返回一些 XML。当它工作并与服务器通信时,我正在显示加载程序动画。
问题是我看不到动画。要么我做错了什么,要么网络连接速度很快。
有谁知道如何在我的ajax代码中引入延迟以减慢进程并且我可以测试动画功能?
谢谢。
ps:ajax代码如下,以防万一
function fetchData($nodeid, $area){
if( $nodeid != $currentRoomId ){
popupBox($area);//$area, $roomInfo);
}
var $nid, $title, $classroom, $boardroom, $cabaret;
$.ajax({
url: "/room/" + $nodeid + "/rss.xml",
dataType: "xml",
success: function($xml){
$returnXML = $xml;
$($xml).find('node > *').each(
function(){
switch( $(this).attr('name') ){
case 'Nid':
$nid = $(this).text();
break;
case 'Title':
$title = $(this).text();
break;
case 'Classroom':
$classroom = $(this).text();
break;
case 'Boardroom':
$boardroom = $(this).text();
break;
case 'Cabaret':
$cabaret = $(this).text();
break;
case 'Theatre':
$theatre = $(this).text();
break;
case 'Notes':
$notes = $(this).text();
break;
default:
break;
}//close switch statement
}
);
$roomInfo = new Array();
$roomInfo.push('Title', $title);
$roomInfo.push('Classroom', $classroom);
$roomInfo.push('Boardroom', $boardroom);
$roomInfo.push('Cabaret', $cabaret);
$roomInfo.push('Theatre', $theatre);
$roomInfo.push('Notes', $notes);
highlightRow($nid);
popupBox($area, $roomInfo);
},
statusCode: {
200: function(){
//alert('responding just fine!');
}
},
error: function(){
//alert('Ajax not responding!');
},
complete: function(){
//alert('completed!');
}
});
}
I have a small ajax jquery script which returns some XML for me. Whilst it is working and communicating with the server I am displaying a loader animation.
The problem is I don't see the animation. Either I have done something wrong or the network conection is really quick.
Does anyone know how I can introduce a delay to my ajax code to slow the process down and I can test the animation feature?
Thank you.
ps: ajax code is below, just incase
function fetchData($nodeid, $area){
if( $nodeid != $currentRoomId ){
popupBox($area);//$area, $roomInfo);
}
var $nid, $title, $classroom, $boardroom, $cabaret;
$.ajax({
url: "/room/" + $nodeid + "/rss.xml",
dataType: "xml",
success: function($xml){
$returnXML = $xml;
$($xml).find('node > *').each(
function(){
switch( $(this).attr('name') ){
case 'Nid':
$nid = $(this).text();
break;
case 'Title':
$title = $(this).text();
break;
case 'Classroom':
$classroom = $(this).text();
break;
case 'Boardroom':
$boardroom = $(this).text();
break;
case 'Cabaret':
$cabaret = $(this).text();
break;
case 'Theatre':
$theatre = $(this).text();
break;
case 'Notes':
$notes = $(this).text();
break;
default:
break;
}//close switch statement
}
);
$roomInfo = new Array();
$roomInfo.push('Title', $title);
$roomInfo.push('Classroom', $classroom);
$roomInfo.push('Boardroom', $boardroom);
$roomInfo.push('Cabaret', $cabaret);
$roomInfo.push('Theatre', $theatre);
$roomInfo.push('Notes', $notes);
highlightRow($nid);
popupBox($area, $roomInfo);
},
statusCode: {
200: function(){
//alert('responding just fine!');
}
},
error: function(){
//alert('Ajax not responding!');
},
complete: function(){
//alert('completed!');
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 setTimeout 引入延迟。
You can use the setTimeout to introduce a delay.
如果您可以控制服务器代码,则可以通过在服务器代码中添加延迟来使服务器延迟 AJAX 响应,而不是在客户端中造成延迟。
If you have control of your server code, you could make the server delay the AJAX response by adding a delay in your server code, rather than causing a delay in the client.
快速解决方案是将 ajax 调用移至 if 块内。成功/错误时 - 隐藏动画。这不会模拟延迟,但会确保动画得到显示(即使是毫秒)。
Quick solution is to move your ajax call inside the if block. On success/error - hide the animation. That won't simulate a delay, but it'll ensure that your animation gets displayed (even if for ms).