jquery progressbar – 一次加载所有内容

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

有任何想法吗?

网页:

<input type="hidden" id="uid" name="UPLOAD_IDENTIFIER" value="" >

Process.php – 在提交表单时调用

  

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请求调用:

  

AFAIK,PHP会话实际上是同步的。 这意味着Process.php脚本阻止getProgress.php脚本运行,直到Process.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/en/function.session-write-close.php 。

会话数据通常在脚本终止后存储而不需要调用session_write_close(),但是由于会话数据被锁定以防止并发写入,因此任何时候只有一个脚本可以在会话上运行。 […]

我没有测试过以下代码,因为我现在无法访问服务器,但我想有些事情应该可以工作: