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!');
            }
        });
    }

您可以使用setTimeout引入延迟。

如果您可以控制服务器代码,您可以通过在服务器代码中添加延迟来使服务器延迟AJAX响应,而不是在客户端中造成延迟。

快速解决方案是将ajax调用移动到if块中。成功/错误时-隐藏动画。这不会模拟延迟,但可以确保显示动画(即使是毫秒)。

最新更新