【问题标题】:Countdown timer that counts down the execution of php script倒计时 php 脚本执行倒计时
【发布时间】:2014-04-17 20:49:22
【问题描述】:

我正在尝试在我的脚本上添加一个正在检查多个网站的某些因素的 COUNTDOWN,这将让用户知道,在脚本完成任务之前,还剩多少小时/分钟/秒。我需要对我的 php 脚本执行此操作。但是真的不知道从哪里开始。我只知道可以计算使用microtime() 函数执行的脚本的时间。我尝试在这里搜索很多脚本,但我几乎没有找到与一些动态进度条相关的东西(但解释得不是很好),也没有找到可以倒计时脚本执行时间的动态倒计时计时器。 非常感谢与此问题相关的任何帮助(链接、函数、脚本...)

【问题讨论】:

    标签: javascript php jquery


    【解决方案1】:

    如果不知道脚本的作用,就无法提前知道运行需要多长时间。即使我们确实知道它的作用,也无法知道它运行的确切时间。如果这都在同一个会话中,您可以在脚本中的某些要点放置“% 完成标记”。我在几个地方这样做。它是一个很好的进度条,我还显示了总运行时间。如果您想要这样的内容,请继续阅读...

    (这是使用 jQuery UI progress bar

    如果您的脚本是一个循环,并且每次迭代基本上都在做同样的事情,那么进度条的增长将会非常流畅。如果不是这种情况,并且你的脚本做很多事情非常快,然后一些事情非常慢,那么你的进度条就会像几乎所有 Windows 进度条一样 :) 它可以很快到达某个点然后挂在那里一会儿。

    不是在我的电脑上,所以我确定下面有错别字,但你应该能够明白这一点。希望这会有所帮助...

    这样的东西会出现在你长时间运行的脚本中......

    $_SESSION["time_start"] = microtime(true);    
    $_SESSION["percentComplete"] = 0;
    ... some of your php script ...
    $_SESSION["percentComplete"] = 10;
    ... some of your php script ...
    $_SESSION["percentComplete"] = 20;
    ... some of your php script ...
    $_SESSION["percentComplete"] = 30;
    ... etc ...
    $_SESSION["percentComplete"] = 100;
    die();
    ?> //end of your php script
    

    但如果你的长时间运行的脚本是一个循环,它会更像这样......

    $loopCnt = 0;
    //your php loop
    {
        $loopCnt = $loopCnt+1;
        //...the meat of your script...
        $_SESSION["percentComplete"] = round(($loopCnt/$totalRecordCount)*100);
    }
    $_SESSION["percentComplete"] = 100;
    die();
    ?> //end of your php script
    

    然后在页面上,用户与 ajax ping 交互可以设置为每隔几秒左右检查一次$_SESSION["percentComplete"] 的值,并根据结果使进度条增长。比如……

    function checkScriptProgress()
    {
        $.ajax({
            url: "checkScriptProgress.php",
            type: 'get',
            cache: false,
            success: function( data, textStatus, jqXHR) {
                //(1) MOVE THE PROGRESS BAR
                //if you are using a jQuery UI progress bar then you could do something like...
                //jQuery("#yourProgressBar").progressbar( "option", "value", data.progress );
                //if you are using HTML5 progress bars it would look like...
                //var pBar = document.getElementById("yourProgressBar");
                //pBar.value = data.progress;
    
                //(2) UPDATE ELAPSED TIME
                //if you want to display the total run time back to the user then use: data.totalRunTime
    
                //(3) If report is not finished then ping to check status again
                if (data.progress < 100) setTimeout("checkScriptProgress();", 1000); //check progress every 1 second so the progress bar movement is fluid
            })
        });
    }
    

    那么checkScriptProgress.php 文件看起来像...

    <?php
        header('Content-Type: application/json');
        $time_end = microtime(true);
        $time = $time_end - $_SESSION["time_start"];
        echo '{"progress":'.$_SESSION["percentComplete"].',"totalRunTime":'.$time.'}';
        die();
    ?>
    

    【讨论】:

    • 最令人印象深刻的回应,将对此进行测试,是的,我的脚本是基于循环的。非常感谢。
    • 我完成了实现但没有工作,在我点击提交按钮后的 firebug 上,ajax 脚本正在跟随 echo'ed {"progress":,"totalRunTime":1398676762.7611},并且请让我知道checkScriptProgress.php 和我的循环之间的链接是什么(循环位于一个名为 multipr.php 的单独文件中)。该脚本是一个多页面排名检查器,如下所示:checkprg.com/multi-pagerank-checker.html(目前我没有在实时站点上实现您的解决方案,仅在本地主机上)。
    【解决方案2】:

    在取得任何进展之前,初步估计需要估计完成每个操作需要多长时间以及要执行的操作数量。 (这个公式假设处理不是并行进行的)

    final_timestamp = start_timestamp + (number_of_actions * average_duration_per_action)
    

    记录进度,包括已完成的操作数和到目前为止花费的总时间。这使您可以更新估算值。

    updated_final_timestamp = start_timestamp + (current_timestamp - start_timestamp) /  (num_action_completed / num_actions_total)
    

    【讨论】:

      【解决方案3】:

      在开始任务之前,您永远无法确定任何执行任务的持续时间。那就是artificial intelligence 成为您算法的一部分的时候。您需要使用大量数据来训练您的算法,以便预测任务的持续时间。

      例如,您可以创建应用程序主要部分的 INSERT 语句,然后使用 microtime() 开始计算时间。当插入语句与任何其他预处理完成后,您将将此数据保存为训练数据。

      现在在您的应用程序中......当有人尝试使用相同的流程时。例如,您会说大约需要33 seconds。并且您使用您的 javascript 代码控制动态进度条在 33 秒后处于 100% 状态!

      代码示例:

      <?php
          //** this is done in development phase **//
          function MyFunction($arg1, $arg2){
              // calculate the current time.
              $startTime =microtime(true);
      
              // My Process in here ......
      
              $final_time  = microtime(true) - $startTime;
      
              // Save final time for future usage.
              // Repeate the step 100 times with random data
          }
      
      
          // Now you can pre calculate the average of this task
          // based on the previous training data. 
          function MyFunction($arg1, $arg2){
              // Get avarege executing time for this.. 
              // Tell the user that this function will take ? amount of time. 
          }
      
      
      
      ?>
      

      这种方法比在做任务时计算时间更可靠、更有效。

      • 您永远不需要计算任务中间的时间,因为您已经计算过了。
      • 您永远不需要每秒都会访问您的服务器的 ajax
      • 您的进度条移动顺畅,因为您知道它所花费的时间。

      但是,如果您只需要一个随每一行代码而变化的动态进度条,您将需要在 PHP 代码中管理您的 javascript。

      最后,如果一行比其他行花费更多的时间,最后一种方法会导致问题。您的进度条可能会在 3 秒内达到 %90。并在该状态下停止 10 秒,直到它移动到 100%,这似乎是不真实的。

      • 您需要计算开始前的时间
      • 完成后您需要计算时间
      • 您需要计算两个时间戳之间的差异。
      • 您将动态增加进度条 Un。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多