milestone.js 1012 B

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