views:

134

answers:

3

I want to modify this code so that the value is a variable and so that the progress bar refreshes in real time (or the smallest meter of time possible - milliseconds)

I am going to "seed" the current time to drive the updates

<script type="text/javascript">
$(function() 
{             
    $("#container").progressbar({ value: 0 });
});
</script>


Fosco's edit: (currently not working)

    <script type="text/javascript">
    updateProgress(0);
    function updateProgress(newvalue) 
    {
        $("#container").progressbar({ 'value': newvalue });
        newvalue = newvalue + 1
        if (newvalue < 101) setTimeout('updateProgress(' + newvalue + ');',100);
    }
    </script>
+2  A: 

Problem is, what would be driving this refresh? That's all you need to know. What value will be changing?

Wherever that is changing, change the value of the progressbar.

$("#container").progressbar({ value: 10 }); 

seeding based on time you say?...

function updateProgress(newvalue) {
    $("#container").progressbar({ 'value': newvalue });
    newvalue = newvalue + 1
    if (newvalue < 101) setTimeout('updateProgress(' + newvalue + ');',100);
}

then your initial function to start it would be:

updateProgress(0);

give that a shot.

Fosco
time drives the refresh... i want to "seed" the time
CheeseConQueso
Then you can use a setInterval or setTimeout function to achieve this.
Fosco
not working as expected.... see the edit? am i doing something wrong?
CheeseConQueso
make sure the initial execution is wrapped in a $(document).ready(function() {}); block.. let me know..
Fosco
+1  A: 

The first bit is easy

<script type="text/javascript">
$(function(value) 
{             
    $("#container").progressbar({ value: value });
});
</script>

The next bit not so easy. The purpose of a progress bar is to show progress, so what is it showing the progress of? You should call this function from code that is performing whatever action it is reflecting, as often as you can.

If you just want to loop the code, then set a timeout to call the function after 1 ms, and repeat.

Worth mentioning that the actual smallest time resolution of most browsers is not 1 ms, but quite a bit less.

James Gaunt
that's basically what i was looking for, thanks
CheeseConQueso
CheeseConQueso
the timeout for 1ms calls a function, the function updates the progress bar and then calls the timeout again, to the same function.... the hard part here is stopping the loop. but that's where the actual thing you are monitoring should come in. you could monitor a global variable to stop the loop. another problem is that if you don't tie this into the actual code being monitored, how do you know how far you are through the process (i.e. whether you are at 50% or 90%) - but that's up to you of course. good luck.
James Gaunt
fyi some work by the legendary john resig on browser time resolution, if high resolution is really important :http://ejohn.org/blog/accuracy-of-javascript-time/
James Gaunt
accepted on "set a timeout to call the function after 1ms" - add more detail (actual code) about that in your answer
CheeseConQueso
+1  A: 

By 'real time', I assume you mean that you want to update it as some series of steps complete. If you are looking to update the progress bar based on the passing of time, I recommend just using some kind of animated graphic. Otherwise, you could reach the end of your progress bar before the function actually completes, which would be confusing for your users.

I used the progress bar in an application where I had three tasks to perform to complete an action. Each task was submitted with its own ajax call, and each included the following in its success handler:

$('.progress').progressbar('option', 'value', $('.progress').progressbar('option', 'value') + 33);

That is, I added 33 to the current value of the progress bar each time one of the tasks was finished.

im not worried about confusion as i will reset this bar at its completion - im using it more as a loop/cycle bar than a progress
CheeseConQueso