I am eager to halt the JavaScript countdown when it reaches zero

BigDay = new Date("November 14, 2013 14:50:55") 

function countdown(){ 
    gtoday = new Date();
    msPerDay = 24 * 60 * 60 * 1000;
    timeLeft = (BigDay.getTime() -gtoday.getTime());
    e_daysLeft = timeLeft / msPerDay;
    daysLeft = Math.floor(e_daysLeft);
    e_hrsLeft = (e_daysLeft - daysLeft)*24;
    hrsLeft = Math.floor(e_hrsLeft);
    e_minsLeft = (e_hrsLeft -hrsLeft)*60;
    minsLeft = Math.floor(e_minsLeft);
    e_secsLeft = (e_minsLeft - minsLeft)*60;
    secsLeft = Math.floor(e_secsLeft);

    document.getElementById('offer2').innerHTML = "<H4>" + daysLeft +
    ":Days " + hrsLeft +":Hrs " + minsLeft + ":Mins " + secsLeft + ":Secs
    Left</H4> ";

    setTimeout(function(){countdown()},1000); 
}
setTimeout(function(){countdown()},1000);

Answer №1

subsequent

timeElapse = (BigEvent.getTime() - today.getTime());

include

if(timeElapse <=0 ) {
    return;
}

or if you desire to display 0 at the end:

BigEvent = new Date("November 14, 2013 14:50:55")

function timer(){ 
    today = new Date();
    millisecondsPerDay = 24 * 60 * 60 * 1000;
    timeElapse = (BigEvent.getTime() - today.getTime());
    estimatedDaysLeft = timeElapse / millisecondsPerDay;
    daysRemaining = Math.max(0, Math.floor(estimatedDaysLeft));
    estimatedHoursLeft = (estimatedDaysLeft - daysRemaining)*24;
    hoursRemaining = Math.max(0, Math.floor(estimatedHoursLeft));
    estimatedMinutesLeft = Math.max(0, (estimatedHoursLeft -hoursRemaining)*60);
    minutesRemaining = Math.floor(estimatedMinutesLeft);
    estimatedSecondsLeft = Math.max(0, (estimatedMinutesLeft - minutesRemaining)*60);
    secondsRemaining = Math.floor(estimatedSecondsLeft);

    document.getElementById('offer2').innerHTML = "<H4>" + daysRemaining +
    ":Days " + hoursRemaining +":Hrs " + minutesRemaining + ":Mins " + secondsRemaining + ":Secs Left</H4> ";

    if(timeElapse <=0 ) {
        return;
    }
    setTimeout(function(){timer()},1000); 
}
setTimeout(function(){timer()},1000);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Inner Workings of the Java Map.containsKey() Function

I have encountered an issue while using a Map<Map, Boolean> in Java. When I try to validate the Map with the containsKey() method, it always returns false, even though the inner Map is constantly updating its keys and values. Below is a sample code s ...

The code is throwing an error stating that it is unable to determine the length of an

My code is throwing the following error: cannot read property length of undefined in arr[i].length function filterArray(arr, elem) { let newArray = []; for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { ...

YourKit: Can you explain the concept of "objects not belonging to a specific web application"?

When using the YourKit Java profiler tool, you may come across a recorded category in the Snapshot called Web Application. Within this category, objects are identified that do not belong to a specific web application. What does it imply for users of the ...

What is the best way to execute operations on two distinct datasets within a single function using RxJS?

Is there a way to perform operations on two separate data collections within a single function in RxJS, returning an observable instead of void? This function is located within a service and I intend to subscribe to it from my component. I believe some re ...

I can't quite understand the reasoning behind why this specific function is designed to output

I've been working on a JavaScript exercise and struggling to understand the logic behind it. The exercise involves a function named "mystery" that utilizes several basic functions to return an array in reversed order. Despite spending hours trying to ...

Transforming the jQuery tooltip to be shown in a column layout

Hello, I am currently using the displayTag library to showcase some tables. My goal is to include a tooltip on each display:column element by utilizing jQuery. Below is the code snippet in question: <c:set var="titleName"><wp:i18n key="FILENAME" ...

Fix the incremental values in an array (JavaScript)

I'm currently working on adjusting the incremented value in my dataset. Here's a glimpse of what I have: const data = [1,2,4,5,6,7]; const correctResult = [1,2,3,4,5,6]; What I want to achieve is to increment each value by 1 while maintaining th ...

Material UI Card shadow effect getting cropped

Currently experiencing an uncommon issue while using Material UI's Card component - the box-shadow is cut off on the top and bottom sides. Any suggestions on how to resolve this problem? Check out my code below: import React, { Component } from & ...

Simple CSS for creating a "red alert badge" with a number count

Looking for the best way to display the popular red notification indicator with count in a consistent manner across various browsers. It seems tricky to achieve a design that looks seamless on all platforms, as different browsers interpret paddings differe ...

JavaScript and Memory Allocation: Working with Array Elements

If I may ask a brief question driven by curiosity: imagine having an object with an array as a property, like this.arr = ["one", "two", "three", ...], and let's say this array is filled with n elements. What will occur to these elements if we execute ...

Combining two RxJs observables to create selectable options for a material drop-down menu

I'm encountering issues while attempting to combine two different observables and display the results in a Material Select component. This example (created using the Material docs tool) demonstrates what I'm trying to achieve. However, the optio ...

Three.js - Error: Attempting to access the 'normal' property of an undefined value

Encountered an issue that I'm struggling with Uncaught TypeError: Cannot read property 'normal' of undefined upon executing the following code on my live website: var text_geo = new THREE.TextGeometry("H", {size:20}); var text_mat = new ...

Incorporating Earth Engine scripts into my AngularJS project to showcase NDVI data layer on a Google Map

Is there anyone who has successfully integrated the Earth Engine API into their front-end JavaScript code? I've been attempting to follow the demo on the earth-engine repository to add a layer to a map, but I haven't had any success. It seems lik ...

Accessing and streaming an audio file located on the server

I've been exploring ways to hide audio files (mp3) from users on my website that has different permission levels. The goal is to play specific audio files without storing them in the webroot directory for security reasons. To prevent direct access lik ...

What is the reason my answer for the powerset problem is flawed? Both my recursive and iterative methods are attached for review

How can I generate all possible subsets from an array of unique integers? For instance, if I have powerSet[1,2,3], the expected output should be [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]] I've tried a recursive approach: function powerset(arr ...

Preventing the page from refreshing when submitting a form

I'm trying to implement a form with a checkbox that functions as a toggle switch. Here's the code I have so far: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style ...

I am experiencing issues with the detection of mouseover events on an SVG circle

I am currently working on a d3 map with zoom functionality that displays circles representing different cities. However, I am facing an issue where the mouseover event for the circles (which shows tooltips and clicking reveals some lines) seems to only reg ...

Exploring the Depths of Google Chrome: Unleashing the Power of

While a similar question has been posed previously, I am encountering difficulties debugging Javascript in Google Chrome. When I navigate to Page > Developer, the "Debug Javascript" function (Ctrl+Shift+L) is not active. Even trying Alt + ` does not se ...

Tips for automatically updating a static reference list

Here is a simplified version of a problem I am facing in my Java service. Please let me know if anything is unclear. The following (pseudo) code is executed in the java service where the user calls 'isItemPresent()'. This function is called freq ...

The selenium automated tool clicks on an item, leading to a redirect to a new page where it attempts to locate the identical item from

Review my code for a method I created to wait for elements in selenium webdriver. I implement it like this: waitForElement(By.id(idOfElement)); public void waitForElement(By by) { for (int second = 0;; second++) { //System.out.println("second ...