当前位置: 首页 > 面试题库 >

jQuery progressbar-一次加载所有

长孙玉泽
2023-03-14
问题内容

我想要一个jQuery进度栏,该进度栏会根据服务器端请求的状态进行更新。我基于本教程编写此代码,但它使用文件上传器作为基础(与此问题相同)。没有文件上传器,我无法使其工作完全相同。问题是进度条仅在process.php完成后才更新。它等待整个过程完成,而不是异步请求进度更新。我只看到数据:数据警报一次。

有任何想法吗?

网页:

<form id="upload-form" action='process.php' method="post" target="upload-frame">
<input type="hidden" id="uid" name="UPLOAD_IDENTIFIER" value="<?php echo $uid; ?>" >
<input type="submit" value="Submit" />
</form>

<div id="progressbar"></div>

<iframe id="upload-frame" name="upload-frame" style="display:none"></iframe>

Process.php-提交表单时调用

<?php
session_start();
$varArray=array(1,2,3,4);
$_SESSION['total']=count($varArray);

foreach($varArray as $val){
    $_SESSION['current']=$val;
    sleep(2);
}
?>

javascript

$(document).ready(function() {
    var started = false;// This flag determines if the upload has started
    $(function() {
        // Start progress tracking when the form is submitted
        $('#upload-form').submit(function() {
            $('#progressbar').progressbar();// Initialize the jQuery UI plugin

            // We know the upload is complete when the frame loads
            $('#upload-frame').load(function() {
                // This is to prevent infinite loop
                // in case the upload is too fast
                started = true;
                // Do whatever you want when upload is complete
                alert('Upload Complete!');
            });

            // Start updating progress after a 1 second delay
            setTimeout(function() {
                // We pass the upload identifier to our function
                updateProgress($('#uid').val());
            }, 1000);
        });
    });

    function updateProgress(id) {
        var time = new Date().getTime();
        // Make a GET request to the server
        // Pass our upload identifier as a parameter
        // Also pass current time to prevent caching
        $.ajax({
            url: 'getProgress.php',
            type: "GET",
            cache: false,
            data: {'uid':id}, 
            dataType: 'text',
            success: function(data){
                alert("data: " + data);
                var progress = parseInt(data, 10);
                if (progress < 100 || !started) {
                    // Determine if upload has started
                    started = progress < 100;
                    // If we aren't done or started, update again
                    updateProgress(id);
                }
                // Update the progress bar percentage
                // But only if we have started
                started && $('#progressbar').progressbar('value', progress);
            }
        });
    }
}(jQuery));

getProgress.php-由ajax请求调用:

<?php
session_start();
if (isset($_REQUEST['uid'])) {
    if (isset($_SESSION['total']) && isset($_SESSION['current'])) {
        // Fetch the upload progress data
        $total = $_SESSION['total'];
        $current = $_SESSION['current'];
        // Calculate the current percentage
        $percent_done = round($current/$total*100);
        echo $percent_done;
    }else{
        echo 100;// If there is no data, assume it's done
    }
}
?>

问题答案:

AFAIK,PHP会话实际上是同步的。这意味着在会话完成Process.php之前,Process.php脚本将阻止getProgress.php脚本运行。

所以发生的是:

  1. Process.php启动并调用session_start()
  2. 服务器将会话控制赋予session_start()
  3. getProcess.php启动并调用session_start()
  4. 服务器将阻塞getProcess.php,直到未使用会话为止。
  5. Process.php完成并关闭会话。
  6. 服务器恢复getProcess.php并赋予其对会话的控制权。
  7. getProcess.php现在可以看到该过程已完成。

参见http://www.php.net/manual/zh/function.session-write-
close.php

会话数据通常在脚本终止后存储,而无需调用session_write_close(),但是由于会话数据被锁定以防止并发写入,因此任何时候任何会话都只能对一个脚本进行操作。[…]

由于目前无法访问服务器,因此我尚未测试以下代码,但我想类似的东西应该可以工作:

<?php
$varArray=array(1,2,3,4);
session_start();
$_SESSION['total']=count($varArray);
session_write_close ();

foreach($varArray as $val){
    session_start();
    $_SESSION['current']=$val;
    session_write_close ();
    sleep(2);
}
?>


 类似资料:
  • 本文向大家介绍js怎么控制一次加载一张图片,加载完后再加载下一张相关面试题,主要包含被问及js怎么控制一次加载一张图片,加载完后再加载下一张时的应答技巧和注意事项,需要的朋友参考一下 参考回答: (1)方法1 alert('图片的宽度为:'+obj.width+';图片的高度为:'+obj.height); (2)方法2    

  • 问题内容: 我正在swift 3.0中的一个项目中醒来,在该项目中,我使用NSCache将服务器的响应缓存在UITableView中,从而缓存了这些响应。但是由于某种原因,当我第一次加载该应用程序时,我只会看到很少的图像加载,但是如果我滚动并返回时,我会看到所有内容(从服务器检索响应结束时,我也重新加载了tableview,但是似乎并非如此)。我不确定我到底在这里缺少什么,下面的代码展示了我如何缓

  • 问题内容: 尝试在运行时向我的Web应用程序中的所有MySQL Select查询添加注释。 例如,代码中的原始查询如下所示: 所有这些都需要 在运行时 修改为: 该应用程序在Hibernate 4.2.1上运行。我能想到的唯一解决方案是扩展并在中添加。 对于为实现此目的而修改的方法有些困惑。希望任何朝着正确方向的指针。 可以重写in方法来做到这一点吗? 编辑1:自定义MySQL方言中的transf

  • 问题内容: 我有一个yeoman脚手架应用程序(全栈角度生成器)。 可以正常工作,但是会产生锁定内存的分布,这很可能是因为角度中的圆形引用。 我将angular升级到了。我得到的错误是: 升级之前,错误为: 调试非常困难,因为它仅在构建/缩小之后才发生。我所有的模块都是angular的数组格式,因此最小化DI应该不是问题,而是这样。 没有单个脚本会导致这种情况。它唯一消失的方法是,如果我不使用我的

  • 我问这个问题,尽管它已经被问了很多次,因为我找了几个小时没有找到解决办法。 但这还不是全部:我工作区中的每个项目都有同样的问题。我什么都跑不了。起初我以为这是因为我在Eclipse启动时犯了一个错误,所以我关闭了它并重新打开了它,但我在正确的工作区中。 我试图清理/重建它,但不起作用。 我试图扰乱属性>Java构建路径,但不起作用。 我试图用一个简单的“Hello World”创建一个新项目,但不

  • 问题内容: 我正在编写一个应用程序,并试图将简单的AJAX功能绑定在一起。它在Mozilla Firefox中运行良好,但是Internet Explorer中有一个有趣的错误:每个链接只能单击一次。浏览器必须完全重新启动,只是重新加载页面将无法进行。我编写了一个非常简单的示例应用程序来演示这一点。 Javascript转载如下: 任何帮助,将不胜感激。 问题答案: 发生这种情况是因为Intern