ultimix
jquery.timer.widget.js
Go to the documentation of this file.
1 
6 if( !ultimix )
7 {
8  var ultimix = {};
9 }
10 
16 if( !ultimix.timer )
17 {
18  ultimix.timer = {};
19 }
20 
21 jQuery.extend(
22  ultimix ,
23  {
24  timer_widget : function( Element , Options )
25  {
26  this._element = Element;
27  this.timeout = Options.timeout;
28  this.timeout_callback = Options.timeout_callback;
29 
30  this.init();
31  }
32  }
33 );
34 
35 ultimix.timer_widget.prototype._prepending_zero = function( Value )
36 {
37  if( Value < 10 )
38  {
39  return( '0' + Value );
40  }
41  else
42  {
43  return( '' + Value );
44  }
45 }
46 
47 ultimix.timer_widget.prototype.init = function()
48 {
49  obj = this;
50  this.set_timer( this.timeout );
51  this.timeout_callback_was_launched = false;
52 
53  this.start_time = Math.floor( ( new Date() ).getTime() / 1000 );
54  window.setInterval( function(){ obj.on_tick( obj ) } , 1000 );
55 }
56 
57 ultimix.timer_widget.prototype.set_timer = function( Time )
58 {
59  Hours = Math.floor( Time / ( 60 * 60 ) );
60  Minutes = Math.floor( Time / 60 - Hours * 60 );
61  Seconds = Math.floor( Time - Hours * 60 * 60 - Minutes * 60 );
62  jQuery( this._element ).empty();
63  jQuery( this._element ).append(
64  '<span class="hours">' + this._prepending_zero( Hours ) + '</span>:<span class="minutes">' +
65  this._prepending_zero( Minutes ) + '</span>:<span class="seconds">' + this._prepending_zero( Seconds ) +
66  '</span>'
67  );
68 }
69 
70 ultimix.timer_widget.prototype.on_tick = function( obj )
71 {
72  Diff = Math.floor( ( new Date() ).getTime() / 1000 ) - obj.start_time;
73  if( obj.timeout - Diff > 0 )
74  {
75  obj.set_timer( obj.timeout - Diff );
76  }
77  else
78  {
79  obj.set_timer( 0 );
80  if( obj.timeout_callback_was_launched == false )
81  {
82  if( obj.timeout_callback )
83  {
84  obj.timeout_callback();
85  }
86  obj.timeout_callback_was_launched = true;
87  }
88  }
89 }
90 
91 jQuery.fn.timer_widget = function( Options )
92 {
93  Options = jQuery.extend(
94  {
95  timeout : 600 , /*in seconds*/
96  timeout_callback : false
97  } ,
98  Options
99  );
100 
101  return jQuery.each(
102  function()
103  {
104  new ultimix.timer_widget( jQuery( this ) , Options );
105  }
106  );
107 }