Code Monkey home page Code Monkey logo

javascript-timer's Introduction

Javascript countdown timer

It converts a javascript timestamp into days, hours, minutes and seconds

Install

Using NPM OR YARN

npm install javascript-timer

OR

yarn add javascript-timer

Usage

You can either include index.js on your html file, or you can simply copy its contents to you main js file

// use a future date (keep it reasonable)

const timestamp = Date.parse("2022-04-11T10:20:30Z"); // return a timestamp in ms

const timeObject = calculateTimeUntil(timestamp) 
/*
  returns an object with 4 key-value pairs i.e 
  {
    days: Number, 
    hrs: Number,
    mins: Number, 
    seconds: Number
  }
*/

You can then use setInterval to populate a countdown elements(s) on your DOM

Example

<script src = '/js/helpers.js'></script>
<div class = 'timer'>
  <div class = 'timer_value' id = 'days'>
    <span class = 'timer_unit'></span>
    <label class = 'timer_label'>Days</label>
  </div>
  <span class = 'timer_divider'>:</span>
  <div class = 'timer_value' id = 'hrs'>
    <span class = 'timer_unit'></span>
    <label class = 'timer_label'>Hours</label>
  </div>
  <span class = 'timer_divider'>:</span>
  <div class = 'timer_value' id = 'mins'>
    <span class = 'timer_unit'></span>
    <label class = 'timer_label'>Minutes</label>
  </div>
  <span class = 'timer_divider'>:</span>
  <div class = 'timer_value' id = 'seconds'>
    <span class = 'timer_unit'></span>
    <label class = 'timer_label'>Seconds</label>
  </div>
</div>

<script>
  function fillUnits(labels, obj) {
    labels.forEach(label => {
      let id, num;
      id = label.id;
      num = obj[id];
      num = num < 10 ? `0${num}` : num;
      label.children[0].textContent = num;
    });
  }

  function populateCountDown(timer, num) {
    let labels, timeTo;
    timeTo = calculateTimeUntil(num);
    labels = Array.from(timer.children).filter(function(label){
      return label.classList.contains('timer_value')
    });
    fillUnits(labels, timeTo)
  }

  (function() {
    const timer = document.currentScript.previousElementSibling;
    const timestamp = Date.parse("2022-04-11T10:20:30Z"); // return a timestamp in ms
    populateCountDown(timer, timestamp);
    setInterval(function(){
      populateCountDown(timer,timestamp)
    }, 1000);            
  })();                  
                  
</script>

javascript-timer's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.