| 1 |
/* global MilestoneConfig */ |
| 2 |
|
| 3 |
var Milestone = ( function( $ ) { |
| 4 |
var Milestone = function ( args ) { |
| 5 |
var $widget = $( '#' + args.id ), |
| 6 |
id = args.id, |
| 7 |
refresh = args.refresh * 1000; |
| 8 |
|
| 9 |
this.timer = function() { |
| 10 |
var instance = this; |
| 11 |
|
| 12 |
$.ajax( { |
| 13 |
url: MilestoneConfig.api_root + 'jetpack/v4/widgets/' + id, |
| 14 |
success: function( result ) { |
| 15 |
$widget.find( '.milestone-countdown' ).replaceWith( result.message ); |
| 16 |
refresh = result.refresh * 1000; |
| 17 |
|
| 18 |
if ( ! refresh ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
setTimeout( |
| 23 |
function() { |
| 24 |
instance.timer(); |
| 25 |
}, |
| 26 |
refresh |
| 27 |
); |
| 28 |
} |
| 29 |
} ); |
| 30 |
|
| 31 |
}; |
| 32 |
|
| 33 |
if ( refresh > 0 ) { |
| 34 |
this.timer(); |
| 35 |
} |
| 36 |
}; |
| 37 |
return function ( args ) { |
| 38 |
return new Milestone( args ); |
| 39 |
}; |
| 40 |
} )( jQuery ); |
| 41 |
|
| 42 |
( function() { |
| 43 |
var i, MilestoneInstances = {}; |
| 44 |
|
| 45 |
if ( typeof( MilestoneConfig ) === 'undefined' ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
for ( i = 0; i < MilestoneConfig.instances.length; i++ ) { |
| 50 |
MilestoneInstances[i] = new Milestone( MilestoneConfig.instances[i] ); |
| 51 |
} |
| 52 |
} )(); |
| 53 |
|