One of the nice things about JavaScript is that it can be so easy to write a
general function for something like this.
To make a version of setInterval that also calls the function immediately,
all you need is:
function nowInterval( fn, time ) {
fn();
return setInterval( fn, time );
}
And then your code would be:
$(function() {
nowInterval( function() {
$('#result').load('/someurl');
}, 10000 ); // repeat now and every 10 seconds
});
-Mike
> From: Pops
>
> Blair, this is a great idea.
>
> I don't know if you considered this and maybe I did it wrong
> myself since I'm still new to jQuery, but when I prepare a
> time like so:
>
> $(function() {
> setInterval( function() {
> $('#result').load('/someurl');
> }, 10000 ); // repeat every 10 seconds
> });
>
> What bugs me is that triggers it only the 10 seconds is
> reached. I would like for it to start the event immediately
> and from that point
> on, do the interval event. In short, in the example above, the user
> has to wait 10 seconds before anything happens. To get around this I
> had to call the url first:
>
> $('#result').load('/someurl');
> $(function() {
> setInterval( function() {
> $('#result').load('/someurl');
> }, 10000 ); // repeat every 10 seconds
> });
>
> Something trivial of course, but a pain in the butt (a PITA
> <g>) to have to "duplicate" a line, sort of anti-jQuery. :-)