I'm trying to display the countdown timer between an enddate/time column and the system date/time column using below code. It's not displaying the timer.
I have created a page item P3_TIMER and it has a column P3_STARTDATE.
var timer;
var endDate = new Date();
endDate.setDate(endDate.getDate()); //End date is the sys date
timer = setInterval(function() {
timeBetweenDates(endDate);
}, 1000);
function timeBetweenDates(toDate) {
var dateEntered = :P3_STARTDATE;
var now = new Date();
var difference = dateEntered.getTime() - now.getTime();
if (difference <= 0) {
// Timer done
clearInterval(timer);
} else {
var seconds = Math.floor(difference / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
$("#days").text(days);
$("#hours").text(hours);
$("#minutes").text(minutes);
$("#seconds").text(seconds);
}
$s('P3_TIMER',timer);
}
It's not possible to mix pl/sql and javascript. They're different languages and run in different environments.
function timeBetweenDates(toDate) {
var dateEntered = :P3_STARTDATE; >>> This is pl/sql
var now = new Date();
The P3_STARTDATE needs to be converted to a javascript date object. That cannot be directly, some parsing is needed as shown in this thread.
For the example below the assumption is made that the date is passed in format DD-MON-YYYY.
var timer;
var endDate = new Date();
endDate.setDate(endDate.getDate()); //End date is the sys date
timer = setInterval(function() {
timeBetweenDates(endDate);
}, 1000);
function parseDate(s) {
var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
var p = s.split('-');
return new Date(p[2], months[p[1].toLowerCase()], p[0]);
}
function timeBetweenDates(toDate) {
var dateEntered = parseDate(apex.item( "P63_DATE" ).getValue() );
var now = new Date();
var difference = dateEntered.getTime() - now.getTime();
if (difference <= 0) {
// Timer done
clearInterval(timer);
} else {
var seconds = Math.floor(difference / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
$("#days").text(days);
$("#hours").text(hours);
$("#minutes").text(minutes);
$("#seconds").text(seconds);
}
apex.item("P63_TIMER").setValue(`Days: ${days}, Hours: ${hours}, Minutes: ${minutes}, Seconds: ${seconds}`);
}
Related
I can't get the second countdown function to work correctly in the first Snippet. I fire off cdtd(); a second time for that. Both cdtd(); functions do not collide and are in inside anonymous functions. When I fire off cdtd(); the first time I get a working countdown timer until 16:00:00. When I fire off it for the second time I will not get a working countdown timer until 17:00:00. This is the actual use case of this question.
Just to made an example flow I made a second snippet. Both cdtd(); do collide eachother but the second cdtd(); function call will give a working countdown timer back. Now I still don't know why it's not working in the first snippet. Havint there a timeOut function I work with.
I'm not sure what is wrong. Anyone has got a clue?
Here is the script. https://jsfiddle.net/3fq2j6a1/
I tried to run the countdown scripts under eachother and that works but I don't need that :) function cdtd() { .. }
Here is a snippet:
// First session
var sessie1 = new Date();
var totsessie1 = new Date(sessie1.getFullYear(), sessie1.getMonth(), sessie1.getDate(), 14, 16, 0, 0) - sessie1;
if (totsessie1 < 0) {
totsessie1 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function() {
document.getElementById("test1").innerHTML = "Message.";
// First countdown
function cdtd() {
var now = new Date();
var dolazak = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 16, 0, 0);
var timeDiff = dolazak.getTime() - now.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = '';
}
//if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById('time').innerHTML = " Still " + hours + " hours, " + minutes + " minutes and " + seconds + " seconds to go!";
timer = setTimeout(cdtd, 1000);
}
cdtd();
// End first countdown
}, totsessie1);
// Second session
var sessie2 = new Date();
var totsessie2 = new Date(sessie2.getFullYear(), sessie2.getMonth(), sessie2.getDate(), 14, 17, 0, 0) - sessie1;
if (totsessie2 < 0) {
totsessie2 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function() {
document.getElementById("test1").innerHTML = "Message.";
// Second countdown
function cdtd() {
var now = new Date();
var dolazak = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 17, 0, 0);
var timeDiff = dolazak.getTime() - now.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = '';
}
//if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById('time').innerHTML = " Still " + hours + " hours, " + minutes + " minutes and " + seconds + " seconds to go!";
timer = setTimeout(cdtd, 1000);
}
cdtd();
// End second countdown
}, totsessie2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="test1">This is a paragraph.</p>
<span id="time"></span>
Here is the snippet of the code when I use the countdown scripts under eachother.
function cdtd() {
var now = new Date();
var dolazak = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 15, 0, 0);
var timeDiff = dolazak.getTime() - now.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = '';
}
//if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById('time').innerHTML = " Still " + hours + " hours, " + minutes + " min and " + seconds + " seconds to go! (countdown to line 49)";
timer = setTimeout(cdtd, 1000);
}
cdtd();
function cdtd() {
var now = new Date();
var dolazak = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 16, 0, 0);
var timeDiff = dolazak.getTime() - now.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = '';
}
//if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById('time').innerHTML = " Still " + hours + " hours, " + minutes + " min and " + seconds + " seconds to go! (countdown to line 49)";
timer = setTimeout(cdtd, 1000);
}
cdtd();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="test1">This is a paragraph.</p>
<span id="time"></span>
https://jsfiddle.net/edfcLzhw/
But I need them inside the session blocks. When doing that I will get a different time left with the second countdown timer. totsessie1(); totsessie2();
The countdown script comes from: Javascript countdown defined with hours
I solved this for now. I did not make the code correctly on 2 points.
First thing. I used the function totsessie1() also being the second totsessie1() function. I changed the second to totsessie2(). I also didn't use clearInterval(timer); before running the second cdtd();.
Here is a working snippet of the end goal:
// First session
var sessie1 = new Date();
var totsessie1 = new Date(sessie1.getFullYear(), sessie1.getMonth(), sessie1.getDate(), 15, 25, 0, 0) - sessie1;
if (totsessie1 < 0) {
totsessie1 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function() {
document.getElementById("test1").innerHTML = "Message.";
// First countdown
function cdtd() {
var now = new Date();
var dolazak = new Date(now.getFullYear(),now.getMonth(),now.getDate(),16,0,0);
var timeDiff = dolazak.getTime() - now.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = '';
}
//if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById('time').innerHTML = " Still " + hours + " hours, " + minutes + " minutes and " + seconds + " seconds to go!";
timer = setTimeout(cdtd, 1000);
}
cdtd();
// End first countdown
}, totsessie1);
// Second session
var sessie2 = new Date();
var totsessie2 = new Date(sessie2.getFullYear(), sessie2.getMonth(), sessie2.getDate(), 15, 26, 0, 0) - sessie2;
if (totsessie2 < 0) {
totsessie2 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function() {
document.getElementById("test1").innerHTML = "Message.";
// Second countdown
clearInterval(timer);
function cdtd() {
var now = new Date();
var dolazak = new Date(now.getFullYear(),now.getMonth(),now.getDate(),17,0,0);
var timeDiff = dolazak.getTime() - now.getTime();
if (timeDiff <= 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = '';
}
//if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById('time').innerHTML = " Still " + hours + " hours, " + minutes + " minutes and " + seconds + " seconds to go!";
timer = setTimeout(cdtd, 1000);
}
cdtd();
// End second countdown
}, totsessie2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>
<p id="test1">This is a paragraph.</p>
<span id="time"></span>
</body>
</html>
JSFiddle: https://jsfiddle.net/nwvk8h12/
I am struggling to make this code to work with localStorage so if anyone can help me that would be amazing. How do I implement a localStorage in order to save the countdown when refreshing the page?
var hour = 5 * 3600;
var minute = 5 * 60;
var deadline = hour + minute;
function formatTime(seconds) {
var hide = false;
var h = Math.floor(seconds / 3600),
m = Math.floor(seconds / 60) % 60;
return h + m;
}
var counter = setInterval(timer, 1000);
function timer() {
deadline--;
if (deadline < 0) {
return clearInterval(counter);
}
$('#deadline').html(formatTime(deadline));
}
Store the value in the localStorage everytime the value gets decreased.
When you reload the page, check if the stored time exists, if yes store it in your deadline variable, otherwise let the deadline be the initial one.
var hour = 5 * 3600;
var minute = 5 * 60;
var deadline = hour + minute;
function formatTime(seconds) {
var hide = false;
var h = Math.floor(seconds / 3600),
m = Math.floor(seconds / 60) % 60;
console.log(h)
console.log(m)
return h + m;
}
var counter;
var storedTime = localStorage.getItem('deadline')
if (storedTime) {
deadline = Number(storedTime)
}
counter = setInterval(timer, 1000);
function timer() {
--deadline;
localStorage.setItem('time', deadline);
if (deadline < 0) {
return clearInterval(counter);
}
$('#deadline').html(formatTime(deadline));
}
I'm creating a countdown Timer that counts down to a date that is inputted in the code for example April 6th 2016.
So far I have got it to output the amount of days, but I cannot figure out how to do the amount of months and years. I do not need Hours Minutes or seconds!
code in app.js
$(document).ready(function(){
eventTime = '6 April 2016';
})
Code in countdown.js:
(function($){
$.fn.countdown = function(options) {
var settings = { date: null };
if (options) {
$.extend(settings, options);
}
this_sel = $(this);
function count_exec () {
eventDate = Date.parse(settings['date']) / 1000;
currentDate = Math.floor($.now () / 1000);
seconds = eventDate - currentDate
days = Math.floor(seconds / (60 * 60 * 24));
months = Math.floor(seconds / (60 * 60 * 12));
alert(days);
}
count_exec();
}
})(jQuery);
Given two dates, use the following code to compute their difference in milliseconds, then in seconds, minutes, hours, days and months:
var currentDate = new Date();
var eventDate = new Date(2016, 3, 6); // months start from 0
var milliseconds = eventDate.getTime() - currentDate.getTime();
var seconds = parseInt(milliseconds / 1000);
var minutes = parseInt(seconds / 60);
var hours = parseInt(minutes / 60);
var days = parseInt(hours / 24);
var months = parseInt(days / 30);
seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;
days -= months * 30;
For a more accurate difference in months, take a look at Difference in Months between two dates in JavaScript.
I currently have many issues trying to think of how to create this script.
I currently have 2 variables :
$curHour = "15:25:00";
$endHour = "16:25:00";
I am struggling to find how to create a countdown specifcally between these times.
I would appreciate any help.
Greetings Glenn
I have just finished in javascript, but the code seems a little long.
html:
<div id="process"></div>
javascript:
var process = document.getElementById('process');
var cur = '15:25:00';
var end = '16:25:00';
cur = cur.split(':');
end = end.split(':');
var x = parseInt(cur[0], 10)*3600+parseInt(cur[1], 10)*60+parseInt(cur[2], 10);
var y = parseInt(end[0], 10)*3600+parseInt(end[1], 10)*60+parseInt(end[2], 10);
showTime(y);
var timer = setInterval(function(){
y--;
showTime(y);
if(y<=x){
clearInterval(timer);
}
}, 1000);
function showTime(diff){
var hour = parseInt(diff/3600, 10);
var minute = parseInt((diff - hour*3600)/60, 10);
var second = diff - hour*3600-minute*60;
hour = hour<10 ? '0'+hour : hour;
minute = minute<10 ? '0'+minute : minute;
second = second<10 ? '0'+second : second;
var time = hour+':'+minute+':'+second;
process.innerHTML = time;
}
You can try like below,
setInterval(function () {
var end = <?php echo strtotime('16:25:00')*1000;?>;//Javascriptcomapatible time
var current = new Date().getTime(); //Current timestamp
var seconds_left = (end - current) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
console.log(days + "d, " + hours + "h,"+ minutes + "m, " + seconds + "s");
}, 1000);
Hi I'm trying to add several javascript countdowns to a single html page. I have included the .js file below. Right now my page only displays the first countdown.
function cdtd () {
var end = new Date("December 25, 2013 00:01:00");
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %=60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('cdtd()',1000);
}
function countdown () {
var end = new Date("May 31, 2013 00:01:00");
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %=60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('countdown()',1000);
}
function cdtd3 () {
var end = new Date("December 25, 2013 00:01:00");
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %=60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('cdtd3()',1000);
}
function cdtd4 () {
var end = new Date("December 25, 2013 00:01:00");
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %=60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('cdtd4()',1000);
}
function cdtd5 () {
var end = new Date("December 25, 2013 00:01:00");
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %=60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('cdtd5()',1000);
}
function cdtd6 () {
var end = new Date("December 25, 2013 00:01:00");
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %=60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('cdtd6()',1000);
}
There are several problems you need to fix here:
Each of your countdown timers uses the same element IDs when it stores the time strings. That's why only one of them shows up.
If any of your countdowns reaches zero, the document.write() call will erase the entire page.
The code is repeated over and over again. This should be one common function for all your countdowns. (What if you need to add one more? Ten more?)
While multiple timers would work, you don't need them. Run a single timer and update all your displayed times from it.
When you call setInterval(), it's better to pass a function reference as the first parameter instead of a string.
Give those ideas some thought and see what you can come up with, then report back with your new code. :-)
Right now you are referencing one box overwriting each value everytime you call the function.
You want to use a query selector instead of getElementById.
Here is a simple example using jQuery:
var cdtd = function(id,end) {
var start = new Date();
var timeDiff = end.getTime() - start.getTime();
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
$( id + " .days").html(days);
$( id + " .hours").html(hours);
$( id + " .minutes").html( minutes);
$( id + " .seconds").html( seconds );
console.log(id + " .hoursBox",$( id + " .hoursBox").length,id,end,hours,minutes,seconds)
var timer = setTimeout(function(){cdtd(id,end)},1000);
}
cdtd("#counter1",new Date("December 25, 2014 00:01:00"));
cdtd("#counter2",new Date("October 31, 2014 00:01:00"));
cdtd("#counter3",new Date("January 1, 2014 00:01:00"));
http://plnkr.co/8LrtWvfGnZWyRYl2RlWd
#Shanimal i m used this code on my website,problem is that when timer reached 0000 then it goes another page and shows deal ended.what i want ,when timer reach 0000 it should must display message on same page on specific div.it should not go for another page.i have tried it by removing
if (timeDiff <= 0) {
clearTimeout (timer);
document.write("Deal Ended");
}
it does not shows any message even when it reached 0000 it start to count again from -1 - 1 -1 -1