Advertisement
rAthus

JavaScript timer live countdown to timestamp

May 17th, 2023 (edited)
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // example : <span timer="578556600"></span>
  2.  
  3. function updateTimer($this) {
  4.     var ts_timer = $this.attr('timer')*1;
  5.     var ts_now = Math.round(Date.now()/1000);
  6.     var s_total = Math.max(ts_timer-ts_now,0);
  7.     var h = Math.floor(s_total/3600);
  8.     var m = Math.floor((s_total-h*3600)/60);
  9.     var s = s_total-h*3600-m*60;
  10.     $this.html((h?h+':':'')+((h||m)?(h?('0'+m).slice(-2):m)+':':'')+((h||m)?('0'+s).slice(-2):s));
  11.     if (!s_total) {
  12.         var uid = $this.attr('uid')
  13.         clearInterval(intervalTimer[uid]);
  14.         if ((typeof(wheelState)=='undefined' || !wheelState) && !jQuery('#popup-akn-card-drawn').is(':visible') && !jQuery('#popup-akn-ad').is(':visible') && !jQuery('#popup-akn-recaptcha').is(':visible') && !jQuery('#popup-akn-auto-roll').is(':visible')) {
  15.             jQuery('#updating-notice').stop(true).fadeTo(250,1);
  16.             setTimeout(function() {
  17.                 location.reload();
  18.             },1500);
  19.         }
  20.     }
  21. }
  22. var intervalTimer = {};
  23. jQuery('[timer]').each(function() {
  24.     var $this = jQuery(this);
  25.     var uid = btoa(Math.random().toString()).substring(10,15);
  26.     $this.attr('uid',uid);
  27.     updateTimer($this);
  28.     intervalTimer[uid] = setInterval(function() {
  29.         updateTimer($this);
  30.     },1000);
  31. });
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement