Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Web Development
You last visited: Today at 12:04

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



countdown timer

Discussion on countdown timer within the Web Development forum part of the Coders Den category.

Closed Thread
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2011
Posts: 147
Received Thanks: 5
countdown timer

how to make the countdown stop when the time is over, it keeps going in -
also how to make it show text or img when its over

Code:
function myTimer() {
  var ending = jQuery("#timer").attr("data-endtime"),
      endTime = new Date(ending);
  endTime = Date.parse(endTime) / 1000;

  var now = new Date();
  now = Date.parse(now) / 1000;

  var timeLeft = endTime - now;

  var days = Math.floor(timeLeft / 86400);
  var hours = Math.floor((timeLeft - days * 86400) / 3600);
  var minutes = Math.floor((timeLeft - days * 86400 - hours * 3600) / 60);
  var seconds = Math.floor(
    timeLeft - days * 86400 - hours * 3600 - minutes * 60
  );

  if (days < "10") {
    days = "0" + days;
  }
  if (hours < "10") {
    hours = "0" + hours;
  }
  if (minutes < "10") {
    minutes = "0" + minutes;
  }
  if (seconds < "10") {
    seconds = "0" + seconds;
  }

  $("#timer").html(
    "<span id='days'>" +
    days +
    "<span>Days</span></span>" +
    "<span id='hours'>" +
    hours +
    "<span>Hrs</span></span>" +
    "<span id='minutes'>" +
    minutes +
    "<span>Mins</span></span>" +
    "<span id='seconds'>" +
    seconds +
    "<span>Secs</span></span>"
  );
}

setInterval(function() {
  myTimer();
}, 1000);
HTML:

Code:
<div id="timer" data-endtime="10 8eptember 2019 9:56:00 GMT+01:00"></div>
ahmed1234550 is offline  
Old 02/12/2018, 22:59   #2


 
iMostLiked's Avatar
 
elite*gold: 1337
Join Date: Apr 2013
Posts: 6,480
Received Thanks: 3,190
setInterval() returns an id, which you can use for clearInterval()

Do it like this:
Code:
var intervalID = 0;

function myTimer() { .. }

intervalID = setInterval(function() {
  myTimer();
}, 1000);
Then you add this in your myTimer() function at the bottom:
Code:
if (timeLeft <= 0){ // when time is 0 or below
    $("#timer").html("Finished!"); // shows text when finished
    clearInterval(intervalID); // stops timer
}
Full code:
iMostLiked is offline  
Thanks
1 User
Old 02/13/2018, 17:15   #3
 
type.'s Avatar
 
elite*gold: 74
Join Date: Aug 2017
Posts: 284
Received Thanks: 151
Quote:
Originally Posted by ahmed1234550 View Post
Code:
  if (days < "10") { days = "0" + days; }
  if (hours < "10") { hours = "0" + hours; }
  if (minutes < "10") { minutes = "0" + minutes; }
  if (seconds < "10") { seconds = "0" + seconds; }
Shouldn't it be < "1" instead of < "10" ?
But as far as behaviour goes the solution from iMostLiked works fine. (:
type. is offline  
Old 02/13/2018, 22:29   #4
 
elite*gold: 0
Join Date: Apr 2011
Posts: 147
Received Thanks: 5
Quote:
Originally Posted by iMostLiked View Post
setInterval() returns an id, which you can use for clearInterval()

Do it like this:
Code:
var intervalID = 0;

function myTimer() { .. }

intervalID = setInterval(function() {
  myTimer();
}, 1000);
Then you add this in your myTimer() function at the bottom:
Code:
if (timeLeft <= 0){ // when time is 0 or below
    $("#timer").html("Finished!"); // shows text when finished
    clearInterval(intervalID); // stops timer
}
Full code:
thank you so much
ahmed1234550 is offline  
Old 02/14/2018, 12:59   #5



 
Serraniel's Avatar
 
elite*gold: 0
The Black Market: 205/1/0
Join Date: May 2010
Posts: 6,853
Received Thanks: 5,106
#Closed
Serraniel is offline  
Closed Thread


Similar Threads Similar Threads
Warcraft 3 Timer countdown
12/22/2012 - General Gaming Discussion - 0 Replies
ok so i know how to edit basic values in cheat engine and editing all the different types of values, but i keep having one problem. the only value i can't seem to find is a countdown timer. tried everything to find the address but i keep missing something. creature re-spawn timers is what i'm referring to. anybody know what i might have to do to find the address?
[HTML/JS] Countdown Timer
12/16/2012 - Coding Tutorials - 6 Replies
Hallo, in diesem Tutorial werde ich euch erklären wie ihr einen Countdown in HTML und JavaScript erstellt. Zuerst erstellt ihr eine 'index.html' und schreibt folgendes rein: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trans itional.dtd"> <head> <title>Countdown Timer</title> <script type="text/javascript" src="countdown.js"></script> </head> <body onload="updateTime()">
#Python Countdown Timer bei Du wirst mit dem Server verbunden
08/13/2011 - Metin2 Private Server - 0 Replies
Hallo com wie ihr euch sicher errinern könnt gab es in Crank´s Client diesen Countdown - Timer der beim connecten von 5-0 gezählt hat und dann erst Fehler beim verbinden mit dem Server ausgegeben hat ich habe folgendes Problem ich will so ein script schreiben aber immer wenn ich das tue aktualisiert sich das Du wirst mit dem Server verbunden 5 nicht es wartet 5 sekunden ohne was anzuzeigen und dann springt es sofort in das else statement hier mein Code: else:...
Df countdown timer
08/02/2010 - Dekaron Private Server - 0 Replies
i found a code here for a count down timer for dead front on this post but it wont let me use 2 of them on the same page (1 timer for each DF). Any one know what to do to fix it?
[Request]CountDown timer
05/27/2010 - CO2 Private Server - 11 Replies
Hey, Can I request the code for a 10 minute countdown timer like Timer T = new timer(); int minutes = 10; int seconds = 0; T.Start(); T.Tick



All times are GMT +1. The time now is 12:05.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.