Skip to content

Commit

Permalink
Added explanation to get seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
walmik committed Jul 21, 2014
1 parent a9e2ad6 commit b0ed660
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ <h4>Usage</h4>
<td>Destroy:</td>
<td>$("#divId").timer("destroy")</td>
</tr>
<tr>
<td>Get number of seconds:</td>
<td>$("#divId").data("seconds")</td>
</tr>
</table>

<br />
Expand Down
45 changes: 30 additions & 15 deletions src/timer.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,29 @@

if(this.options.editable) this.initEditable();

return this;

};

jQueryTimer.prototype.start = function () {
if(!this.isTimerRunning) {
this.updateTimerDisplay();
this.incrementTime(); //to avoid the 1 second gap that gets created if the seconds are not incremented
this.startTimerInterval();
}
this.incrementTime(); //to avoid the 1 second gap that gets created if the seconds are not incremented
this.startTimerInterval();
}

return this;
}

jQueryTimer.prototype.pause = function () {
clearInterval(this.timerId);
this.isTimerRunning = false;
return this;
}

jQueryTimer.prototype.resume = function () {
if(!this.isTimerRunning) this.startTimerInterval();
return this;
}

jQueryTimer.prototype.remove = function () {
Expand All @@ -88,6 +94,7 @@
var self = this;
this.timerId = setInterval(function() { self.incrementTime() }, this.delay);
this.isTimerRunning = true;
return this;
}

/*
Expand Down Expand Up @@ -175,6 +182,8 @@

//assign the number of seconds to this element's data attribute for seconds
this.$el.data('seconds', this.get_seconds());

return this;
}

jQueryTimer.prototype.timeToString = function () {
Expand All @@ -188,24 +197,27 @@
*/
jQueryTimer.prototype.get_seconds = function () {
return ((this.hrsNum*3600) + (this.minsNum*60) + this.secsNum);
}
}

jQueryTimer.prototype.incrementTime = function () {
this.timeToString();
this.updateTimerDisplay();
jQueryTimer.prototype.incrementTime = function () {
this.timeToString();
this.updateTimerDisplay();

this.secsNum++;
if(this.secsNum % 60 == 0) {
this.minsNum++;
this.secsNum = 0;
}
this.secsNum++;
if(this.secsNum % 60 == 0) {
this.minsNum++;
this.secsNum = 0;
}

//handle time exceeding 60 minsNum!
if(this.minsNum > 59 && this.minsNum % 60 == 0)
{
this.hrsNum++;
this.minsNum = 0;
}

return this;

}


Expand All @@ -216,6 +228,7 @@
///////////////INITIALIZE THE PLUGIN///////////////
var pluginName = 'timer';
$.fn[pluginName] = function(options, params) {

return this.each(function() {
/*
Allow the plugin to be initialized on an element only once
Expand All @@ -239,9 +252,9 @@
var instance = $.data(this, 'plugin_' + pluginName);

/*
Provision for calling a function from this plugin
without initializing it all over again
(params will be passed in case the called function needs a param)
Provision for calling a function from this plugin
without initializing it all over again
(params will be passed in case the called function needs a param)
*/
if (typeof options == 'string') {
if (typeof instance[options] == 'function') {
Expand All @@ -252,6 +265,8 @@
instance[options].call(instance, params);
}
}


});
}
////////////////////////////////////////////////////
Expand Down

0 comments on commit b0ed660

Please sign in to comment.