JAVASCRIPT   35

countdown.js

Guest on 10th August 2021 05:00:29 PM

  1. /*
  2. Author: Robert Hashemian
  3. http://www.hashemian.com/
  4.  
  5. You can use this code in any manner so long as the author's
  6. name, Web address and this disclaimer is kept intact.
  7. ********************************************************
  8. Usage Sample:
  9.  
  10. <script language="JavaScript">
  11. TargetDate = "12/31/2020 5:00 AM";
  12. BackColor = "palegreen";
  13. ForeColor = "navy";
  14. CountActive = true;
  15. CountStepper = -1;
  16. LeadingZero = true;
  17. DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
  18. FinishMessage = "It is finally here!";
  19. </script>
  20. <script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
  21. */
  22.  
  23. function calcage(secs, num1, num2) {
  24.   s = ((Math.floor(secs/num1))%num2).toString();
  25.   if (LeadingZero && s.length < 2)
  26.     s = "0" + s;
  27.   return "<b>" + s + "</b>";
  28. }
  29.  
  30. function CountBack(secs) {
  31.   if (secs < 0) {
  32.     document.getElementById("cntdwn").innerHTML = FinishMessage;
  33.     return;
  34.   }
  35.   DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
  36.   DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  37.   DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  38.   DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
  39.  
  40.   document.getElementById("cntdwn").innerHTML = DisplayStr;
  41.   if (CountActive)
  42.     setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
  43. }
  44.  
  45. function putspan(backcolor, forecolor) {
  46.  document.write("<span id='cntdwn' style='background-color:" + backcolor +
  47.                 "; color:" + forecolor + "'></span>");
  48. }
  49.  
  50. if (typeof(BackColor)=="undefined")
  51.   BackColor = "white";
  52. if (typeof(ForeColor)=="undefined")
  53.   ForeColor= "black";
  54. if (typeof(TargetDate)=="undefined")
  55.   TargetDate = "12/31/2020 5:00 AM";
  56. if (typeof(DisplayFormat)=="undefined")
  57.   DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
  58. if (typeof(CountActive)=="undefined")
  59.   CountActive = true;
  60. if (typeof(FinishMessage)=="undefined")
  61.   FinishMessage = "";
  62. if (typeof(CountStepper)!="number")
  63.   CountStepper = -1;
  64. if (typeof(LeadingZero)=="undefined")
  65.   LeadingZero = true;
  66.  
  67.  
  68. CountStepper = Math.ceil(CountStepper);
  69. if (CountStepper == 0)
  70.   CountActive = false;
  71. var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
  72. putspan(BackColor, ForeColor);
  73. var dthen = new Date(TargetDate);
  74. var dnow = new Date();
  75. if(CountStepper>0)
  76.   ddiff = new Date(dnow-dthen);
  77. else
  78.   ddiff = new Date(dthen-dnow);
  79. gsecs = Math.floor(ddiff.valueOf()/1000);
  80. CountBack(gsecs);

Raw Paste


Login or Register to edit or fork this paste. It's free.