How do I change HTML elements with JavaScript? - javascript

I have an external JS file, with the function time().
In my HTML, I have
<p id="sentence"> </p>
<script src="javascript.js" type="text/javascript">
time();
</script>
The time function calculates how much time there is left until a certain hour, I checked with console.log() and the algorithm is good.
I wanna display the output to <p>
I tried:
document.getElementByID("sentence").innerHTML= ("Happy hour starts in " + hour + ":" + minutes + ":" + seconds);
What am I missing?
Edit: the time function as requested:
function time() {
let date = new Date();
date.setHours(16);
date.setMinutes(0);
let day = date.getDate();
date.setSeconds(0)
date.setMilliseconds(0); // set happy hour for the day
let interval = date - Date.now(); //calculate ms between current time and happy hour time
//case when happy hour is later than current date, on the same day
if (interval > 0) {
const hour = Math.floor(interval / 3600000);
interval -= 3600000 * Math.floor(interval / 3600000);
const minutes = Math.floor(interval / 60000)
interval -= 60000 * Math.floor(interval / 60000);
const seconds = Math.floor(interval / 1000);
document.getElementByid("sentence").innerHTML= ("Happy hour starts in " + hour + ":" + minutes + ":" + seconds);
} else if (interval < 0 && interval < -3600000) { //happy hour gone, time left until it starts tomorrow
date.setDate(++day);
console.log(date);
interval = date - Date.now();
const hour = Math.floor(interval / 3600000);
interval -= 3600000 * Math.floor(interval / 3600000);
const minutes = Math.floor(interval / 60000);
interval -= 60000 * Math.floor(interval / 60000);
const seconds = Math.floor(interval / 1000)
document.getElementByid("sentence").innerHTML= ("Happy hour starts in " + hour + ":" + minutes + ":" + seconds);
} else { //happy hour started, but didn't finish yet
const hour = Math.floor(interval / 3600000);
interval -= 3600000 * Math.floor(interval / 3600000);
const minutes = Math.floor(interval / 60000)
interval -= 60000 * Math.floor(interval / 60000);
const seconds = Math.floor(interval / 1000);
document.getElementByid("sentence").innerHTML= ("Happy hour finished in " + hour + ":" + minutes + ":" + seconds);
}
}

Your error are bunch of typos as mentioned in the comments.
All you had to do was: use getElementById instead of getElementByID or getElementByid. (notice how 'Id' is spelt)
Here is a sample working code:
function time() {
let date = new Date();
date.setHours(16);
date.setMinutes(0);
let day = date.getDate();
date.setSeconds(0)
date.setMilliseconds(0); // set happy hour for the day
let interval = date - Date.now(); //calculate ms between current time and happy hour time
//case when happy hour is later than current date, on the same day
if (interval > 0) {
const hour = Math.floor(interval / 3600000);
interval -= 3600000 * Math.floor(interval / 3600000);
const minutes = Math.floor(interval / 60000)
interval -= 60000 * Math.floor(interval / 60000);
const seconds = Math.floor(interval / 1000);
document.getElementById("sentence").innerHTML = ("Happy hour starts in " + hour + ":" + minutes + ":" + seconds);
} else if (interval < 0 && interval < -3600000) { //happy hour gone, time left until it starts tomorrow
date.setDate(++day);
console.log(date);
interval = date - Date.now();
const hour = Math.floor(interval / 3600000);
interval -= 3600000 * Math.floor(interval / 3600000);
const minutes = Math.floor(interval / 60000);
interval -= 60000 * Math.floor(interval / 60000);
const seconds = Math.floor(interval / 1000)
document.getElementById("sentence").innerHTML = ("Happy hour starts in " + hour + ":" + minutes + ":" + seconds);
} else { //happy hour started, but didn't finish yet
const hour = Math.floor(interval / 3600000);
interval -= 3600000 * Math.floor(interval / 3600000);
const minutes = Math.floor(interval / 60000)
interval -= 60000 * Math.floor(interval / 60000);
const seconds = Math.floor(interval / 1000);
document.getElementById("sentence").innerHTML = ("Happy hour finished in " + hour + ":" + minutes + ":" + seconds);
}
}
time()
<p id="sentence"></p>

Assuming javascript.js is a file you need to load, containing the definition of the time function, then this will not work:
<script src="javascript.js" type="text/javascript">
time();
</script>
A script tag with a src attribute will simply load the script from the given URL, and ignore any contents.
It looks like you want to load the file and then call the function: in that case you can do it like this:
<script src="javascript.js" type="text/javascript"></script>
<script>time();</script>

You should use DOM node it's pretty easy especially for your certain use,
Html will be as following
<div id="div1">
</div>
<script src="javascript.js" type="text/javascript">
time();
</script>
Javascript:
var para = document.createElement("p");
var text = document.createTextNode("Happy hour starts in " + hour + ":" + minutes + ":" + seconds);
para.appendChild(text);
var element = document.getElementById("div1");
element.appendChild(para);
Check https://www.w3schools.com/js/js_htmldom_nodes.asp to understand it even better.

Try this instead or pass your function that get detail or hours minutes and seconds
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Time</title>
</head>
<body>
<p id = "sentence"> </p>
<script>
const hour = new Date().getHours();
const minutes = new Date().getMinutes();
const seconds = new Date().getSeconds();
document.getElementById('sentence').innerHTML = "Happy hour starts in " + hour + ":" + minutes + ":" + seconds;
</script>
</body>
</html>
Run the snippet for the output

Related

Countdown Timer every 15-Minutes in the hour

I have a webinar that runs every 15 minutes of every hour of the day (EG: 11:00, 11:15, 11:30 & 11:45).
I'd like a countdown timer that shows the remaining minutes until the next start time and all I can find from days of searches and trying to figure this out myself is an hourly countdown.
My question is, how do I update this code for be every 15 minutes not every 60.
<script>
/* Return minutes and seconds to next hour
** #returns {Object} minutes: minutes remaining
** seconds: seconds remaining
*/
function getTimeRemaining() {
var t = Date.now();
var seconds = (60 - Math.floor(t % 6e4 / 1e3)) % 60;
var minutes = 60 - Math.ceil(t % 3.6e6 / 6e4) + (seconds? 0:1);
return {
'minutes': ('0' + minutes).slice(-2),
'seconds': ('0' + seconds).slice(-2) };
}
// Simple show remaining function
function showRemaining() {
var r = getTimeRemaining();
document.getElementById('clock').textContent = (r.minutes + ':' + ('0' + r.seconds).slice(-2));
// Run again just after next full second
setTimeout(showRemaining, 1020 - (Date.now() % 1000));
}
showRemaining();
</script>
That solution seems overly complicated and you should be definitely using setInterval for a task where you have to update something every x seconds, recursively calling setTimeout is a bad idea. Here is a solution that is much more understandable:
const runEvery = 15 * 60; // 15 minutes in seconds
const showRemaining = () => {
// get current time in seconds and find when the next run starts in seconds
const seconds = Math.round(Date.now() / 1000);
const nextRun = runEvery * Math.ceil(seconds / runEvery);
const timeLeft = nextRun - seconds;
const minutesLeft = Math.floor(timeLeft / 60);
const secondsLeft = timeLeft % 60;
document.getElementById('clock').textContent = minutesLeft + ':' + ('0' + secondsLeft).slice(-2);
}
showRemaining();
setInterval(showRemaining, 1000);
<div id="clock"></div>

Make a JS countdown starts again

I alreay have some code for a countdown, but would like to make it pause for some hours when at 0 (with a text displayed), and then starts again for 14 days.
<script type="text/JavaScript">
var Display=document.getElementById("Counter");
function Countdown() {
var date1 = new Date();
var date2 = new Date ("Oct 20 20:00:00 2017");
var sec = (date2 - date1) / 1000;
var n = 24 * 3600;
if (sec > 0) {
j = Math.floor (sec / n);
h = Math.floor ((sec - (d * n)) / 3600);
mn = Math.floor ((sec - ((d * n + h * 3600))) / 60);
sec = Math.floor (sec - ((d * n + h * 3600 + mn * 60)));
Affiche.innerHTML = "Next game in : " + d +" d "+ h +" h "+ mn +" min "+ sec + " s ";
window.status = "Remaining time : " + d +" d "+ h +" h "+ mn +" min "+ sec + " s ";
}
tCountdown=setTimeout ("Countdown();", 1000);
}
Countdown();
</script>
So to sum up:
1. The countdown reach 0
2. It blocks for 4 hours and display a text ("We are currently playing")
3. It starts again for around 14 days.
I am thinking of something like this to start again the countdown:
var dateX = var date2 + (a length of time around 14 days)
Am I right?
Can I do this only with Javascript?
I broke it up into a bunch of functions so you can reason about it. If you want to test it, you can set the initial seconds in the sec variable to something small, like 10, and then you can also set the second argument in setTimeout to something small, like 10.
<div id="counter"></div>
<script>
// initialize
var first_target_date = new Date ("Oct 20 20:00:00 2017");
var sec = calcSecDiff(new Date(), first_target_date);
var counter = document.getElementById("counter");
var timeout; // we will update this global variable when we want to stop the whole thing
// start the countdown
countdown();
// do it again every second
var interval = setInterval(function(){
countdown();
}, 1000);
function countdown() {
counter.innerHTML = parseTime(sec);
// decrement the second
sec--;
// if we get to 0
if (sec < 0) {
clearInterval(interval);
counter.innerHTML = "We are currently playing";
if (timeout) return; // it's over
var timeout = setTimeout(function(){
sec = daysToSec(14); // reset the seconds to 14 days away
var interval = setInterval(function(){
countdown();
}, 1000);
}, hrsToMs(4)); // wait four hours before counting down again
};
}
// returns days, hours, minutes, and seconds from seconds
// see https://stackoverflow.com/questions/13903897/javascript-return-number-of-days-hours-minutes-seconds-between-two-dates
function parseTime(sec){
// calculate (and subtract) whole days
var days = Math.floor(sec / 86400);
sec -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(sec / 3600) % 24;
sec -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(sec / 60) % 60;
sec -= minutes * 60;
// what's left is seconds
var seconds = sec % 60;
return days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
}
// calculates the difference between two dates in seconds
function calcSecDiff(date1, date2){
return Math.round((date2 - date1) / 1000);
}
// converts hours to milliseconds
function hrsToMs(hrs){
return hrs * 60 * 60 * 1000;
}
// converts days to seconds
function daysToSec(days){
return days * 24 * 60 * 60;
}
</script>

timezone javascript in my timer

I have found a code but i dont know to add timezone . i want to detect the timer from the timezone of the other country like denmark/copenhagen. thank you. this is my code.
<script type="text/javascript">
ElapsedTimeLogger = function(dateElementId, elapsedElementId, hiden, interval) {
var container = $(elapsedElementId);
var time = parseDate($(dateElementId).val());
var interval = interval;
var timer;
function parseDate(dateString) {
var date = new Date(dateString);
return date.getTime();
}
function update() {
var systemTime = new Date().getTime();
elapsedTime = systemTime - time;
container.html(prettyPrintTime(Math.floor(elapsedTime / 1000)));
$(hiden).val(prettyPrintTime(Math.floor(elapsedTime / 1000)));
}
function prettyPrintTime(numSeconds) {
var hours = Math.floor(numSeconds / 3600);
var minutes = Math.floor((numSeconds - (hours * 3600)) / 60);
var seconds = numSeconds - (hours * 3600) - (minutes * 60);
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
var time = hours + ":" + minutes + ":" + seconds;
return time;
}
this.start = function() {
timer = setInterval(function() {update()}, interval * 1000);
}
this.stop = function() {
clearTimeout(timer);
}
}
$(document).ready(function () {
var timeLogger = new ElapsedTimeLogger("#date", "#elapsed","#stoppedid", 1);
timeLogger.start();
$("#confirm").click(function() { //Stop timer upon clicking the Confirm Button
timeLogger.stop();
});
});
</script>
thank you. i dont know javascript. i know php only. i tried to put
before the code is running. i already save a time from europe/copenhagen. but when the timer is running. it says 6:00:01 abd counting.. but i want to run like this 0:00:01 and counting. and my idea the time from europe and time in my country is 6 hours. i want to run the time from europe not my country. because i save the time from europe using php. see bellow the code for save the time.
date_default_timezone_set("Europe/Copenhagen");
but wont work. i didnt found the solution
Analyzing this code, I rewrote the needed HTML to see what the code do. It's simply creates a counter in format hh:mm:ss and shows on screen, this counter show the time passed since the date informed.
to add the user timezone to reflect in your timer, you just need to recalculate the seconds inside the prettyPrintTime(numSeconds) function before use it to get hours, minutes and seconds.
function prettyPrintTime(numSeconds) {
var tzOffset = new Date().getTimezoneOffset(); // get the timezone in minutes
tzOffset = tzOffset * 60; // convert minutes to seconds
numSeconds -= tzOffset; // recalculate the time using timezone
var hours = Math.floor(numSeconds / 3600);
var minutes = Math.floor((numSeconds - (hours * 3600)) / 60);
var seconds = numSeconds - (hours * 3600) - (minutes * 60);
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
var time = hours + ":" + minutes + ":" + seconds;
return time;
}
Take a look at the working code:
https://jsfiddle.net/4c6xdcpr/
function getClientTimeZone() {
var offset = new Date().getTimezoneOffset(),
o = Math.abs(offset);
return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}
// Display Output
alert(getClientTimeZone());

Countdown reset on specific time

Currently I have a script, which has a countdown for a specific date, but I want the countdown to be specific on a timer, so let's say if I start the timer and I have set the timer to run for 30 days, it will then run for 30 days and then reset back to 30 days again and start running. Is it possible to change it to do so?
My code:
<body>
<span id="countdown"></span>
<script LANGUAGE="Javascript">
// set the date we're counting down to
var target_date = new Date("Apr 9, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 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);
// format countdown string + set tag value
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";
}, 1000);
</script>
</body>
EDIT:
I have now changed the code to look like underneath, but now when I open the website in my browser its blank.
New code:
<span id="countdown"></span>
<script LANGUAGE="Javascript">
// set the date we're counting down to
var target_date = new Date("Apr 9, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
if (seconds_left <= 0){
target_date = target_date + 30 days;
}
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 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);
// format countdown string + set tag value
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";
}, 1000);
I really advice you to take advantage of JavaScript libraries , in your case moment JS is the perfect solution, you can check their documentation and see how you can manage time easily. Anyways here is the solution of your question using moment js.
First download moment js and add it to your page.
HTML CODE
<span id="days"> </span>
<span id="hours"></span>
<span id="minutes"></span>
<span id="seconds"></span>
It can not get simple than that :)
JAVASCRIPT
//create two variables for holding the date for 30 back from now using substract
var back30Days = moment().subtract(30, 'days').format('YYYY-MM-DD H:mm:ss');
var countDownSeconds = Math.floor(moment().diff(back30Days, 'seconds'));
//variables holding days, hours , minutes and seconds
var Days, Minutes, Hours, Seconds;
// Set Interval function for performing all calculations and decrementing the countDownSeconds
setInterval(function () {
// Updating Days
Days = pad(Math.floor(countDownSeconds / 86400), 2);
// Updating Hours
Hours = pad(Math.floor((countDownSeconds - (Days * 86400)) / 3600), 2);
// Updating Minutes
Minutes = pad(Math.floor((countDownSeconds - (Days * 86400) - (Hours * 3600)) / 60), 2);
// Updating Seconds
Seconds = pad(Math.floor((countDownSeconds - (Days * 86400) - (Hours * 3600) - (Minutes * 60))), 2);
// Updation our HTML view
document.getElementById("days").innerHTML = Days + ' Days';
document.getElementById("hours").innerHTML = Hours + ' Hours';
document.getElementById("minutes").innerHTML = Minutes + ' Minutes';
document.getElementById("seconds").innerHTML = Seconds + ' Seconds';
// Decrement the countDownSeconds
countDownSeconds--;
// If we reach zero , our chrono should reset to 30 days back again, as you told
if (countDownSeconds === 0) {
countDownSeconds = Math.floor(moment().diff(back30Days, 'seconds'));
}
}, 1000);
// Function for padding the seconds i.e limit it only to 2 digits
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
Here is a jsfiddle

JavaScript count down, add hours & minutes

So, I have the below (seconds countdown) in good order. But! I am trying to add hours & minutes as apart of the count down as well. Ideally keeping the same structure, and just using pure JS. I would like the output to be:
There is X hours, X minutes, and X seconds remaining on this Sale!
var count=30;
var counter=setInterval(timer, 1000); //1000 will run it every 1 second
function timer()
{
count=count-1;
if (count <= 0)
{
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML=count + " secs"; // watch for spelling
}
If the solution has to be a rewrite with jQuery or another library; that's fine. Just not preferable.
Cheers and Salutations for any help.
Something like this:
var count = 30;
var counter = setInterval(timer, 1000); //1000 will run it every 1 second
function timer() {
count = count - 1;
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
document.getElementById("timer").innerHTML = hours + "hours " + minutes + "minutes and" + seconds + " seconds left on this Sale!"; // watch for spelling
}
var totalSeconds = 3723; // lets say we have 3723 seconds on the countdown
// that's 1 hour, 2 minutes and 3 seconds.
var hours = Math.floor(totalSeconds / 3600 );
var minutes = Math.floor(totalSeconds % 3600 / 60);
var seconds = totalSeconds % 60;
var result = [hours, minutes, seconds].join(':');
console.log(result);
// 1:2:3
hours is seconds divided by the number of seconds in hour (3600) rounded down
minutes is the remainder of the above division, divided by the number of seconds in a minute (60), rounded down.
seconds is the remainder of total seconds divided by seconds in a minute.
Each calculation after hour has to use a modulus calculation to get the remainder, because you don't care about total time at that step, just progress to the next tick.
I would use a similar method to the others, but I wouldn't rely on setInterval / setTimeout as a timer, especially if users might be looking at the page for some time, as it tends to be inaccurate.
var endTime = new Date(2013, 10, 31).getTime() / 1000;
function setClock() {
var elapsed = new Date().getTime() / 1000;
var totalSec = endTime - elapsed;
var d = parseInt( totalSec / 86400 );
var h = parseInt( totalSec / 3600 ) % 24;
var m = parseInt( totalSec / 60 ) % 60;
var s = parseInt(totalSec % 60, 10);
var result = d+ " days, " + h + " hours, " + m + " minutes and " + s + " seconds to go!";
document.getElementById('timeRemaining').innerHTML = result;
setTimeout(setClock, 1000);
}
setClock();
This method calculates the difference between now and the date in the future each time it is run, thus removing any inaccuracies.
Here is an example: http://jsfiddle.net/t6wUN/1/

Categories