With arguments.callee wouldn't you still lose the original 'this' ?

Why not a closure, just set 'this' to 'that'. Also, you should return
the jQuery object:

(function($) {
  $.fn.JSClock2 = function() {
                 var that = this;
                  setInterval(function() {
                        var today=new Date(),
                        h=today.getHours(),
                        m=today.getMinutes(),
                        s=today.getSeconds();
                        m=timeFormat(m);
                        s=timeFormat(s);
                        function timeFormat(i) {
                                        if (i < 10) {
                                                        i="0" + i;
                                        }
                        return i;
                        }
                           that.html("Local Time: " + h +":"+ m +":"+s);
                  }, 1000);
             return this;
   }
})(jQuery);

On Apr 22, 9:18 pm, Ricardo <ricardob...@gmail.com> wrote:
> Just add setTimeout(arguments.callee, 1000); to the end of the JSClock
> function (after this.html...). That way the function will repeat
> itself over and over every second.
>
> In case you need to stop it later you'll have to save a reference to
> the timeout:
>
> //at the end of JSClock function
> $.fn.JSClock.timer = setTimeout(arguments.callee, 1000);
>
> //define a sub-method
> $.fn.JSClock.stop = function(){
>   clearTimeout( this.timer );
>
> };
>
> cheers,
> - ricardo
>
> On Apr 22, 8:23 pm, kiusau <kiu...@mac.com> wrote:
>
> > QUESTION:  How do I get a function to repeat itself an unspecified
> > number of times?
>
> > BACKGROUND:  I have created a digital clock with which I very
> > satisfied except for one shortcoming:  it displays only once and stops
> > ticking.  The only way to keep track of the time is to refresh the
> > page.  I have introduced the setInterval( ) function in a variety of
> > ways to compel JSClock() to repeat itself, but to no avail.
>
> > SOURCE CODE:
>
> > (function($) {
> >                   $.fn.JSClock = function() {
> >                                 var today=new Date();
> >                                 var h=today.getHours();
> >                                 var m=today.getMinutes();
> >                                 var s=today.getSeconds();
> >                                 m=timeFormat(m);
> >                                 s=timeFormat(s);
> >                                 function timeFormat(i) {
> >                                         if (i < 10) {
> >                                                 i="0" + i;
> >                                         }
> >                                 return i;
> >                                 }
> >                                 this.html("Local Time: " + h +":"+ m 
> > +":"+s);
> >                         }
>
> > })(jQuery);
>
> > $(document).ready(function() {
> >         $('#clock').JSClock();
>
> > });

Reply via email to