开发者

PHP Curl progress bar (callback returning percentage)

I have implemented the curl progress bar using

curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'callback'开发者_如何学C);

curl_setopt($curl, CURLOPT_BUFFERSIZE,64000);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

and a callback function.

problem is, the script is outputting the percentage on my html everytime like this :

0
0.1
0.2
0.2
0.3
0.4
..
..
..
1
1.1

How do i combine this with CSS to show a changing progress bar ?


Suppose you have a progress bar HTML:

<div id="progress-bar">
    <div id="progress">0%</div>
</div>

CSS:

#progress-bar {
    width: 200px;
    padding: 2px;
    border: 2px solid #aaa;
    background: #fff;
}

#progress {
    background: #000;
    color: #fff;
    overflow: hidden;
    white-space: nowrap;
    padding: 5px 0;
    text-indent: 5px;
    width: 0%;
}

And JavaScript:

var progressElement = document.getElementById('progress')

function updateProgress(percentage) {
    progressElement.style.width = percentage + '%';
    progressElement.innerHTML = percentage + '%';
}

You can have it output JavaScript and have it update the progress bar for you, for example:

<script>updateProgress(0);</script>
<script>updateProgress(0.1);</script>
<script>updateProgress(0.2);</script>
..
..

Note that you can't put each update in separate script block, because the browser will try to read the full script before executing and the progress bar will not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜