是进度条可以用PHP和JavaScript使用AJAX可以用、进度条、PHP、JavaScript

由网友(夏末凉城空余心)分享简介:我想知道如何让如Gmail进度条。我试过<脚本SRC =jquery.js和>< / SCRIPT><脚本>$(函数(){$阿贾克斯({网址:的index.php,成功:功能(数据){$('#巴)HTML(数据)。}});})< / SCRIPT>< D​...

我想知道如何让如Gmail进度条。

我试过

 <脚本SRC =jquery.js和>< / SCRIPT>
<脚本>
$(函数(){
    $阿贾克斯({
        网址:的index.php,
        成功:功能(数据){
            $('#巴)HTML(数据)。
        }
    });
})
< / SCRIPT>
< D​​IV ID =酒吧>< / DIV>
 

和上的index.php

:按睡眠()我只是为了模拟输出一样多线程程序不支持在 PHP 。

 < PHP

为($ i = 0; $ I< = 10; $ I ++)
{
    睡眠(1);
    回声$ I;
}
 

似乎输出是呼应了一次,所以我得到的结果 012345678910 一次。

也是我试图

 的setInterval(函数(){
        $阿贾克斯({
            网址:的index.php,
            成功:功能(数据){
                $('#巴)HTML(数据)。
            }
        });
    },1000);
 
国内外测试用例管理工具有哪些 好用的测试用例管理工具推荐

相反,我很难维持进步的价值,所以我做了

 < PHP

在session_start();

如果(使用isset($ _ SESSION ['值'])){
    如果($ _ SESSION ['值']> = 10)
    {
        取消设置($ _ SESSION ['值']);
    }
    其他
    {
        $ _SESSION ['值'] ++;
    }
}
其他
{
    $ _SESSION ['值'] = 0;
}

回声$ _SESSION ['值'];
 

作为我的PHP的一部分。但似乎,我呼吁连续区间AJAX功能。

我的问题是,

谷歌如何使用进度条,而在Gmail中loginng。他们是否得到连续的'流'数据,如我想在我的第一个例子或发送(定期)的要求对某些URL(尽管不是通过AJAX ..通过JSONP或其他)的和upadate页面就像第二个?

我可以做相同的 PHP ,即使不使用PHP,我可以做到这一点使用JavaScript并在支持多线程其他的服务器端脚本语言?

解决方案

我不知道究竟是什么Gmail不会的进度,但可以实现在PHP中类似的东西,虽然它可能是一个有点棘手。

首先,解释为什么你的例子不工作:

如果您回应和睡眠,就像你的第一个例子,它永远不会工作。 Ajax的执行完全请求 - 即,如果响应没有完成,它将等待。当你循环和睡眠的要求是不是封闭,直到PHP脚本执行完毕。

如果您在其他例如使用会话一样,问题就变得会话存储。这家商店的脚本执行过程中通常是被锁定,并且不会自动更新,以便进步的类型检查你想要的。

你可以做的就是写的进步到一个文件(或数据库)手动。例如:

  file_put_contents(progress.txt',1);
 

然后,还有一个脚本读取文件和输出的内容。

这应该工作,因为file_put_contents打开,写和关闭文件。

使用其他语言比PHP会更容易。依据多线程将可能更容易,但不是必要条件。然而,有一个连续运行的过程将变得更简单(PHP只运行了你的要求的时间进程,然后退出)

I was wondering how to make progress bar like gmail.

I tried

<script src="jquery.js"></script>
<script>
$(function (){
    $.ajax({
        url: 'index.php',
        success: function(data) {
            $('#bar').html(data);
        }
    });
})
</script>
<div id="bar"></div>

And on index.php

[EDIT]: by sleep() i just meant to simulate continuous stream of output like multithreaded programs which is not supported in php.

<?php

for($i=0; $i<=10; $i++)
{
    sleep(1);
    echo "$i";
}

it seems that output is echoed out at once so i get result 012345678910 at once.

also i tried

setInterval(function (){
        $.ajax({
            url: 'index.php',
            success: function(data) {
                $('#bar').html(data);
            }
        });
    }, 1000);

Instead, i had trouble maintaining value of 'progress', so i did

<?php

session_start();

if(isset($_SESSION['value'])){
    if($_SESSION['value'] >= 10)
    {
        unset($_SESSION['value']);
    }
    else
    {
        $_SESSION['value']++;
    }
}
else
{
    $_SESSION['value'] = 0;
}

echo $_SESSION['value'];

as part of my php. But it seems that, i am calling ajax function on continuous interval.

My Question is,

How does google use progress bar, while loginng in gmail. Do they get continuos 'stream' of data like i tried on my first example or send (regularly) request on some url (though not through ajax .. through JSONP or whatever) and upadate the page like second ?

Can I do same with php , even if not with php, can I do it using javascript and other server side scripting language where multithreading is supported?

解决方案

I'm not sure what exactly Gmail does for the progressbar, but you can achieve something similar in PHP, although it may be a bit tricky.

First, to explain why your examples don't work:

If you echo and sleep, like your first example, it will never work. Ajax performs a full request - that is, if the response does not finish, it will wait. When you loop and sleep, the request is not "closed" until the PHP script has finished executing.

If you use session, like in the other example, the problem becomes the session store. The store is typically locked during script execution, and it will not update itself to allow for the type of progress check you want.

What you could do is write progress to a file (or to a database) manually. For example:

file_put_contents('progress.txt', 1);

Then, have another script read the file and output the contents.

This should work, because file_put_contents opens, writes and closes the file.

Using some other language than PHP would make it easier. Being multithreaded would possibly make it easier, but is not a requirement. However, having a continuously running process would make it simpler (PHP only runs a process for the duration of your request and then exits)

阅读全文

相关推荐

最新文章