doujue6196 2017-08-14 09:23
浏览 39

如果退出网页,如何返回ajax请求

How i can back to ajax request? for examplek I enter the webpage, start ajax request and progress bar is moving forwarde and for example I exit this page and back there, how to return to active/started ajax request ? how to not start this function again but if is running just back to this request.

that`s my ajax code:

<script>
    jQuery(document).ready(function($) {


                setTimeout(getProgress,100);
              $('.stan').text('Pobieram analizę...');
              $.urlParam = function(name){
             var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
                            if (results==null){
                               return null;
                            }
                            else{
                               return decodeURI(results[1]) || 0;
                            }
                                    }
        var id = $.urlParam('id');
        var idt = $.urlParam('idt');

                  $.ajax({
                                 url: "views/getresults.php?id="+id+"&idt="+idt,

                    success: function(data) {
                        $("#loadresults").append(data);
                    }
                });
                setTimeout(getProgress,100);

            return false;


            function getProgress(){

                $.ajax({

                    url: 'views/listen.php',
                    cache: false,
                   success: function(data) {
                        if(data<= 95 && data >= 0){
                            console.log(data);
                            $('#loadresults').html(data);
                            $('#progress').attr('aria-valuenow', data);
                            $('#progress').css('width', data + "%");  
                            $('#progress').text(data + "%");    

                            setTimeout(getProgress,1000);
                            console.log('Repeat');

                        }
                         else {
                            $('#progress').attr('aria-valuenow', 100);
                            $('#progress').css('width', 100 + "%");  

                            $('#progress').text(100 + "%");    

                            $('.stan').text('Dane zostały pobrane');
                                                                    setTimeout(function(){
                                        document.getElementById('progress').style.visibility = "hidden";
                                        },1000);
                         setTimeout(function(){
                                        document.getElementById('stan').style.visibility = "hidden";
                                        },1200);


                            console.log('End');
                        }
                    }
                });
            }

        });
        </script>
  • 写回答

1条回答 默认 最新

  • doufuhuang6981 2017-08-15 08:55
    关注

    What you are asking for is technically impossible.

    If you exit a web page (by navigating away from it, or closing the browser), everything on the page is destroyed. Next time you go there, your browser reloads the page again from the server (or possibly, from a cache) and starts again. That's because navigating to a URL in the browser makes a brand new HTTP request to the server. There is no such thing as the "previous" ajax request. Once you navigate away from the page the first time, it's like it never existed. None of the code or content on that previous copy of the page exists any more.

    The web is stateless. This is a key concept you should be aware of. It means that nothing is preserved from one request to the next. The way applications preserve state is by taking active measures to use things like cookies, server-side sessions, databases etc to provide persistent storage. But you cannot do anything equivalent to resurrect an in-progress ajax request which was orphaned by closing its host page.

    Your only workaround is to keep the page open. If the user wants to navigate to another page, by using a link or button on that page, you should ensure that it opens the link in another tab/window. Of course if the user takes independent action (e.g typing a new address in the browser, using back/forward buttons) there is nothing you can do to stop them.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c