Javascript 加载脚本的AJAX进度条

Javascript 加载脚本的AJAX进度条,javascript,php,ajax,Javascript,Php,Ajax,在AJAX中制作进度条有一个大问题。整个页面都是AJAX,其中一个页面是AJAX,它加载一个函数从数据库中获取一些大行 我试图在foreach循环中通过flush()方法并通过写入/读取$\u会话使该脚本中的进度条变为flush()方法,但仍然一无所获。我真的尝试了所有我不知道怎么做的事情。只需要这个来完成我的项目。有人能帮我吗 不管怎样,我想加载哪个脚本,这个进度条的模板如何在GET或POST ajax中使用它,对于任何ajax <script> jQuery(docume

在AJAX中制作进度条有一个大问题。整个页面都是AJAX,其中一个页面是AJAX,它加载一个函数从数据库中获取一些大行

我试图在foreach循环中通过flush()方法并通过写入/读取$\u会话使该脚本中的进度条变为flush()方法,但仍然一无所获。我真的尝试了所有我不知道怎么做的事情。只需要这个来完成我的项目。有人能帮我吗

不管怎样,我想加载哪个脚本,这个进度条的模板如何在GET或POST ajax中使用它,对于任何ajax

<script>
    jQuery(document).ready(function($) {
            
            $(document).on('click','#save',function () {
                setTimeout(getProgress,1000);
              $(this).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,3000);
            return false;
            });
            function getProgress(){
                
                $.ajax({
                    url: 'views/listen.php',
                    success: function(data) {
                        if(data<=100 && data>=0){
                            console.log(data);
                            $('#loadresults').html('<div id="progress"><div class="progress-bar" role="progressbar" style="width:'+ (data / 100)*100 +'%">'+ data + '/' +100+'</div></div>');
                            setTimeout(getProgress,1000);
                            console.log('Repeat');
                        } else {
                            $('#loadresults').text('Pobieram dane');
                            console.log('End');
                        }
                    }
                });
            }
        });
        </script>
还有一个get progress函数listen.php

<?php
session_start();
echo (!empty($_SESSION['progress']) ? $_SESSION['progress'] : '');

if (!empty($_SESSION['progress']) && $_SESSION['progress'] >= $_SESSION['total']) {
    unset($_SESSION['progress']);
}

?>

您无法从ajax获取数据下载的进度。一旦您向服务器请求,下一个响应将仅在获取数据后或发生错误时进行


解决方法是,将数据作为分数。例如,首先下载1/10的数据,在成功回调中,再次递归调用ajax请求2/10的数据。在每次成功回调时,您都可以更改进度条。

查看服务器端事件或长轮询作为选项。

写入和读取会话不起作用,因为PHP的标准行为是在执行主代码时锁定会话文件

尝试创建一个文件并使用函数执行期间完成的百分比更新其内容。比如:

<?php
function slowFunction() {
    $file = uniqid();
    file_put_contents($file, 0);
    // Long while that makes your stuff
    // you have to know how many loops you will make to calculate the progress
    $total = 100;
    $done = 0;
    while($done < $total) {
        $done++;
        // You may want not to write to the file every time to minimize changes of being writing the file 
        // at the same time your ajax page is fetching it, i'll let it to you...
        $progress = $done / $total;
        file_put_contents($file, $progress);
    }
    unlink($file); // Remove your progress file
}

你试过什么?主要是这个会话方法,任何flush,还有HTML5中的spinner,我是ajax新手,我做了很多工作,但是这个进度条是…请显示你的代码,这样我们就可以得到想法。另一件事是,我确切地知道foreach Loop的所有计数结果的数量,所以它与session相同,但使用的是文件。是吗?你能将此代码实现到我的代码中以更好地理解吗?对不起,当我看到你的问题时,没有代码。是的,它和会话是一样的,但是使用文件。您的函数“getProgress”必须侦听该文件,因此,您必须传递its名称,而不是“listen.php”。如何在我的ajax启动函数中获得该名称?好的,在foreach循环中,我在文件中得到了这个add数字,如何得到与listen相同的数字?在foreach循环中实现这个函数?
<?php
function slowFunction() {
    $file = uniqid();
    file_put_contents($file, 0);
    // Long while that makes your stuff
    // you have to know how many loops you will make to calculate the progress
    $total = 100;
    $done = 0;
    while($done < $total) {
        $done++;
        // You may want not to write to the file every time to minimize changes of being writing the file 
        // at the same time your ajax page is fetching it, i'll let it to you...
        $progress = $done / $total;
        file_put_contents($file, $progress);
    }
    unlink($file); // Remove your progress file
}