Using JavaScript, create an interval that calls your scripts.

Marc Wagner, July 22, 2022

You want to show content on your website only at a certain point in time? Then you can do this quickly and easily with JavaScript.

The final code #

If you are an experienced web developer and don’t need long explanations, I have the complete code right here:

"use strict";

window['checktimer'] = function(date, intervalTime, onSuccess, onUpdate){
    let TimerInterval = null;
    let endDate = new Date(date);
    let calls = 0;

    TimerInterval = setInterval(function(){
        calls++;
        var startDate = new Date();

        var seconds = (endDate.getTime() - startDate.getTime()) / 1000;
        if(seconds <= 0){
            if(typeof(onSuccess) !== "undefined") {
                onSuccess(calls);
            }
            clearInterval(TimerInterval);
        }else{
            if(typeof(onUpdate) !== "undefined") {
                onUpdate(calls);
            }
        }
    }, intervalTime);
}

Appropriately, here is the call to the function so you can get started right away:

window.checktimer("2020-11-10 10:50:00", 1000, function(calls){
     # Called as soon as the Timestamp is reached
  }, function(calls){
     # Called whenever an Update has processed.
} );

For all other budding web developers, here is a short explanation. By the way, you can find a code that compares two dates in PHP here.

The explanation of the JavaScript code #

First, we create a function that is globally valid by defining it in the scope of the window.

window['checktimer'] = function(date, intervalTime, onSuccess, onUpdate){}

The function should take four parameters that we need to allow some flexibility:

dateThe formatted date (YYYY-MM-DD HH:ii:ss)
e.g.: “2020-11-10 10:50:00”
intervalTimeThe milliseconds, how often the date is checked.
onSuccessA callback function that is called when the desired date is reached.
onUpdateA callback function that is called when the date has been checked.
Parameters of the function.

Right at the beginning we declare our values, which are only needed for the function itself, but which should not be changed from the outside.

let TimerInterval = null; // store the interval object
let endDate = new Date(date); // store the date object
let calls = 0; // calls done till the date is reached

We then use the values to create the interval to check at regular intervals whether the target date has been reached.

TimerInterval = setInterval(function(){
    calls++;
    var startDate = new Date();

    var seconds = (endDate.getTime() - startDate.getTime()) / 1000;
    if(seconds <= 0){
        if(typeof(onSuccess) !== "undefined") {
            onSuccess(calls);
        }
        clearInterval(TimerInterval);
    }else{
        if(typeof(onUpdate) !== "undefined") {
            onUpdate(calls);
        }
    }
}, intervalTime);

As soon as this is the case, the seconds change from a positive integer to a negative one, which is why we can terminate the interval and call the defined “onSuccess” callback function.

Summary #

Calculating the difference between two dates in seconds is easily done with JavaScript. By offloading the function and passing in callback functions, you can create clean, reusable code that can be built into any web page or web application.

I hope this post helps you with your project. If you have any questions or feedback, feel free to leave a comment.

Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment