Ajax延迟模拟

发布于 2024-11-15 19:07:13 字数 2664 浏览 0 评论 0原文

我有一个小的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没企图 2024-11-22 19:07:13

您可以使用 setTimeout 引入延迟。

You can use the setTimeout to introduce a delay.

梦毁影碎の 2024-11-22 19:07:13

如果您可以控制服务器代码,则可以通过在服务器代码中添加延迟来使服务器延迟 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.

留蓝 2024-11-22 19:07:13

快速解决方案是将 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文