robcowie / jquery-stopwatch

A jQuery plugin that renders a count-up clock from a defined start time

Home Page:http://robcowie.github.com/jquery-stopwatch/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Continues to Count when Element is gone

Nvenom opened this issue · comments

I use this to count a users time spent with a dialog open, and i was feeling a bit lazy to bind a destroy function to all the dialog close functions, so naturally i added a little quick fix. im no expert so im not sure if this is okay or if it will continue to run "render"

Changed

render: function() {
            var $this = $(this),
                data = $this.data('stopwatch');
            $this.html(data.formatter(data.elapsed, data));
        }

To This

render: function() {
            var $this = $(this),
                data = $this.data('stopwatch');
            if(data){
                $this.html(data.formatter(data.elapsed, data));
            }
        }

You want to clear the interval instead

data.tick_function = function() {

                    var millis = data.incrementer();
                    if(!$this.data('stopwatch')) {
                        clearInterval(data.timerID);
                        return;
                    }

                    data.elapsed = millis;
                    data.target.trigger('tick.stopwatch', [millis]);
                    data.target.stopwatch('render');
                };