简体   繁体   English

加载脚本的 AJAX 进度条

[英]AJAX progress bar of load script

I have a big problem to make a progress bar in AJAX.我在 AJAX 中制作进度条有一个大问题。 The whole page is in AJAX, inside one of the webpage is AJAX which loads a function to get some big rows from the database.整个页面都在 AJAX 中,其中一个网页是 AJAX,它加载一个函数以从数据库中获取一些大行。

I tried to make progress bar in this script in a foreach loop a flush() method and by writing/reading to $_SESSION, but still nothing.我试图在 foreach 循环中的这个脚本中创建一个flush() 方法并通过写入/读取 $_SESSION 来制作进度条,但仍然没有。 I really tried everything I don`t know how to do this.我真的尝试了一切我不知道如何做到这一点。 Need only this to complete my project.只需要这个就可以完成我的项目。 Could someone help me with this?有人可以帮我解决这个问题吗?

It is anyway which script I want to load, how is the template for this progress bar to use it in GET or POST ajax, for any AJAX.无论如何,我想加载哪个脚本,对于任何 AJAX,此进度条的模板如何在 GET 或 POST 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>

and here is a getresults.php这是一个 getresults.php

    foreach($result as $resul) {
    $count++;
    session_start();
    $_SESSION['progress'] = $count;
    $_SESSION['total'] = 100;
    session_write_close();
    sleep(1);
}
unset($_SESSION['progress']); 

and a get progress function listen.php和一个获取进度函数 listen.php

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

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

?>

Writing and reading session doesn't work because the standard behavior of PHP is to lock the session file while your main code is being executed.写入和读取会话不起作用,因为 PHP 的标准行为是在执行主代码时锁定会话文件。

Try to create a file and update the its content with the percentage done during the execution of your function.尝试创建一个文件并使用函数执行期间完成的百分比更新其内容。 Something like:类似的东西:

<?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
}

?> ?>

You can't get the progress of the data download from ajax.无法从ajax获取数据下载进度。 Once you request you to the server, the next response will be only after fetching the data or when the error occurs.一旦您向服务器发出请求,下一个响应将仅在获取数据后或发生错误时进行。

The solution to you is, get the data as fractions.您的解决方案是,将数据作为分数。 Such as first download the 1/10th of the data, and in the success callback, recursively call the ajax again requesting the 2/10th data.比如先下载1/10的数据,在成功回调中,再次递归调用ajax请求2/10的数据。 On each success callback, you could change the progress bar.在每次成功回调时,您都可以更改进度条。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM