function refresh() {
var currentDay = new Date();
var breakStart = new Date(2017, 3, 21, 15, 20);
var breakEnd = new Date(2017, 4, 1, 8, 40);
var diff = (breakStart.getTime() - currentDay.getTime()) / 1000;
var diff2 = (breakEnd.getTime() - currentDay.getTime()) / 1000;
dayz = Math.floor(diff2 / 86400)
diff2 %= 86400;
hourz = Math.floor(diff2 / 3600)
diff2 %= 3600;
minutez = Math.floor(diff2 / 60)
diff2 %= 60;
secondz = Math.floor(diff2 / 1)
diff2 %= 1;
days = Math.floor(diff / 86400)
diff %= 86400;
hours = Math.floor(diff / 3600)
diff %= 3600;
minutes = Math.floor(diff / 60)
diff %= 60;
seconds = Math.floor(diff / 1)
diff %= 1;
if (days == 1) {
document.getElementById("final").innerHTML = ("There is " + days + " day, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds left until spring break.");
}
if (days > 1) {
document.getElementById("final").innerHTML = ("There are " + days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds left until spring break.");
}
if (days == 0) {
document.getElementById("final").innerHTML = ("There are " + days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds left until spring break.");
}
if (dayz > 1 && dayz < 9) {
document.getElementById("final").innerHTML = ("We are in break! Hooray! There are " + dayz + " days, " + hourz + " hours, " + minutez + " minutes, and " + secondz + " seconds left in spring break");
}
}
setInterval(refresh, 1000);
<style> #import url('https://fonts.googleapis.com/css?family=Raleway:300');
p {
font-family: 'Raleway', sans-serif;
font-size: 400%;
color: ;
}
</style>
<!DOCTYPEhtml>
<embed src="Chamelion.mp3" autostart="true" loop="true" width="2" height="0">
<center>
<p>SPRING BREAK:</p>
</center>
<center>
<p id="final"></p>
</center>
Hello,
This program I wrote calculates the amount of time it is until spring break. I put the below line of code my html but this does not seem to work to link to my js file. On the code snippet here it works, but i'm trying to use an external script.
Sorry for the big block of code. Anything helps!
Seb
EDIT:
The code as it is in my editor:
<embed src="Chamelion.mp3" autostart="true" loop="true"
width="2" height="0">
</embed>
<script src="spring.js" type="text/javascript"></script>
<style>
#import url('https://fonts.googleapis.com/css?family=Raleway:300');
p{
font-family: 'Raleway', sans-serif;
font-size: 400%;
color: ;
}
</style>
<center><p>SPRING BREAK:</p></center>
<center><p id="final"></p></center>
<center><img src="bunny.jpg" width="300"></center>
the problems seems to be that you've loaded the script before the targeted final exists in document object model.
place the code below as the last line of the <body> element:
<body>
...
...
...
<script src="spring.js" type="text/javascript"></script>
</body>
Place the line at the end just before the tag
...
<script src="spring.js" type="text/javascript"></script>
</body>
</html>
Related
I made this countdown timer to show a video after it reaches the end. However it just goes into negative numbers. Seems to be related to the part of the code to hide the content after expiry date. Here is a JS fiddle
<div id="countdown"></div>
<div id="playsession"></div>
<script>
var releaseDate = new Date('05/29/2021 9:00 UTC+1');
var expiryDate = new Date('10/11/2021 01:00AM UTC+1');
var cdNotice = 'This session will appear automatically when the countdown finishes';
var trDay = ' Days';
var trHours = ' Hours';
var trMin = ' Minutes';
var trSec = ' Seconds';
var media = "<div class=\"wistia_responsive_padding\" style=\"padding:56.25% 0 0 0;position:relative;\"><div class=\"wistia_responsive_wrapper\" style=\"height:100%;left:0;position:absolute;top:0;width:100%;\"><iframe src=\"https:\/\/fast.wistia.net\/embed\/iframe\/eiwj630vxa?videoFoam=true\" title=\"June 19 & 20 ~ Refresh & Revive ~ Gen Rabten ~ 1 Video\" allow=\"autoplay; fullscreen\" allowtransparency=\"true\" frameborder=\"0\" scrolling=\"no\" class=\"wistia_embed\" name=\"wistia_embed\" allowfullscreen msallowfullscreen width=\"100%\" height=\"100%\"><\/iframe><\/div><\/div>\r\n<script src=\"https:\/\/fast.wistia.net\/assets\/external\/E-v1.js\" async><\/script>";
</script>
Above I set the start time and expiry time.
If the person loads the page before the countdown ends it should show the countdown. If the person loads after the countdown it will show the video.
If the person loads the page after the expiry time it should show the expired message.
Timer
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = releaseDate - now;
var gone = expiryDate - now;
if (distance < 0 && gone > 0) {
clearInterval(timer);
document.getElementById('countdown').innerHTML = "";
document.getElementById('playsession').innerHTML = media;
return;
}
if (gone < 0) {
clearInterval(timer);
document.getElementById('playsession').innerHTML = '<p>This video has now expired</p>';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById('countdown').innerHTML = '<p>' + cdNotice + '</p>';
document.getElementById('countdown').innerHTML += '<p>';
if (days > 0) {
document.getElementById('countdown').innerHTML += '<b>' + days + trDay + '</b> ';
}
if (hours > 0) {
document.getElementById('countdown').innerHTML += '<b>' + hours + trHours + '</b> ';
}
if (minutes > 0) {
document.getElementById('countdown').innerHTML += '<b>' + minutes + trMin + '</b> ';
}
document.getElementById('countdown').innerHTML += '<b>' + seconds + trSec +'</b>';
document.getElementById('countdown').innerHTML += '</p>';
}
timer = setInterval(showRemaining, 1000);
Your expire date is incorrect, add space before AM, then it will work, otherwise your condition is not met, because gone = NaN.
With this it works correctly:
var expiryDate = new Date('10/11/2021 01:00 AM UTC+1')
How can I have a countdown timer with predetermined time?
My code
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
var seconds = 6;
var minutes = 1;
function calculate() {
setTimeout(calculate, 1000);
$('#showDate').html(' expires after ' + minutes + ' minutes ' + seconds + ' seconds ');
seconds--;
if (seconds < 0) {
seconds = 59;
minutes--;
if (minutes < 0) {
minutes = 0;
seconds = 0;
}
}
}
calculate();
});
</script>
</head>
<body>
<div id='showDate'></div>
</body>
</html>
The above code applies only to minutes and seconds. How can I add hours and days?
Edited based on the method that was already being used.
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
var days = 4;
var hours = 0;
var minutes = 0;
var seconds = 6;
function calculate() {
setTimeout(calculate, 1000);
$('#showDate').html(' expires after ' + days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds ');
seconds--;
if (seconds < 0) {
seconds = 59;
minutes--;
if (minutes < 0) {
hours--;
minutes = 59;
if (hours < 0) {
days--;
hours = 23;
if (days < 0) {
days = 0;
hours = 0;
minutes = 0;
seconds = 0;
}
}
}
}
}
calculate();
});
</script>
</head>
<body>
<div id='showDate'></div>
</body>
</html>
<html>
<head>
</head>
<body>
<div id="clockdiv">
expires after <span class="days"></span> day <span class="hours"></span> hours <span class="minutes"></span> minutes <span class="seconds">0</span> seconds
<script type="text/javascript">
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);
</script>
</body>
</html>
I'm having a problem with a count down timer made in JavaScript. It was working for the last 2 weeks, but today it started to show NaN:NaN... , and I can't understand why. Here is the code, does anyone have any idea which one could be the problem?
<div id="countdownmain">
<span id="countdownmain" class="timer"></span>
</div>
<script>
var date = new Date;
var secondsnow = date.getSeconds();
var minutesnow = date.getMinutes();
var hournow = date.getHours();
var day = date.getDay();
var passatti = (secondsnow + (minutesnow*60) + (hournow*3600));
if((day==1)||(day==2)||(day==3)||(day==4)){
if(passatti < 46800){
var upgradeTime = 46800 - passatti;
}else if(passatti > 46800){
var upgradeTime = 86400 - passatti + 46800;
}
}else if((day==5)&&(passatti < 46800)){
var upgradeTime = 46800 - passatti;
}else if((day==5)&&(passatti > 46800)){
var upgradeTime = 86400 - passatti + 46800 + 86400 + 86400;
}else if((day==6)){
var upgradeTime = 86400 - passatti + 46800 + 86400;
}else if((day==7)){
var upgradeTime = 86400 - passatti + 46800;
}
var seconds = upgradeTime;
function timer() {
var now = Math.floor(Date.now() / 1000);
('0' + 11).slice(-2)
var days = Math.floor(seconds/24/60/60);
var hoursLeft = Math.floor((seconds) - (days*86400));
var hours = Math.floor(hoursLeft/3600);
var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
var minutes = Math.floor(minutesLeft/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdownmain').innerHTML = "<span class='timesm'> For same working day dispatch, order in </span><br class='appear'><span style='display:inline-block; width:45px;'><span class='hideDays glowW'>" + ('0' + days).slice(-2) + "</span></span><span class='times'> days </span><span style='display:inline-block; width:45px;'><span class='hideHours glowW'>" + ('0' + hours).slice(-2) + "</span></span><span class='times'> hours </span><span style='display:inline-block; width:45px;'><span class='hideMinutes glowW'>" + ('0' + minutes).slice(-2) + "</span></span><span class='times'> minutes </span><span style='display:inline-block; width:45px;'><span class='hideSec glowW'>" + ('0' + remainingSeconds).slice(-2) + "</span></span><span class='times'> seconds </span>";
if (seconds == 0) {
clearInterval(countdownTimer);
//document.getElementById('countdownmain').innerHTML = "Completed";
seconds = upgradeTime;
} else {
seconds--;
$('.hideSec').fadeOut('slow');
}
if(('0' + remainingSeconds).slice(-2)==00){
$('.hideMinutes').fadeOut('slow');
}
if((('0' + minutes).slice(-2)==00)&&(('0' + remainingSeconds).slice(-2)==00)){
$('.hideHours').fadeOut('slow');
}
if((('0' + hours).slice(-2)==00)&&(('0' + minutes).slice(-2)==00)&&(('0' + remainingSeconds).slice(-2)==00)){
$('.hideDays').fadeOut('slow');
}
}
var countdownTimer = setInterval('timer()', 1000);
</script>
Unfortunately, today is Sunday! Javascript return 0 value as first of a week in getDay().
Take a look my fiddle. I just decreased day comparator value in if statement. (e.g. 1 -> 0, 2 -> 1, 3 -> 2 and so on..)
https://jsfiddle.net/mrnf4q7g/
I've been trying to set this fiddle onto two lines such as:
123 20 13 03
Days Hours Min sec
I've tried using br and \n but it tends to staircase down the page.
Are you looking something like that?
If you need any improvements to this answer just comment.
CountDownTimer('October 21, 2015 19:00:00', 'countdown');
function CountDownTimer(date, id) {
var end = new Date(date);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
var showRemaining = function() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = 'EXPIRED!';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById(id).innerHTML = '<div class="FL M5">' + days + '<br> days </div>';
document.getElementById(id).innerHTML += '<div class="FL M5">' + hours + '<br> hours </div>';
document.getElementById(id).innerHTML += '<div class="FL M5">' + minutes + '<br> mins ';
document.getElementById(id).innerHTML += '<div class="FL M5">' + seconds + '<br> secs </div>';
};
timer = setInterval(showRemaining, 1000);
}
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
.FL{
float:left;
}
.CB{
clear:both;
}
.M5{
margin:5px;
}
<div id="countdown"></div>
Did this help?
As xwhtLikeThis said you can use a table like this:
<table>
<tr>
<td>123</td>
<td>20</td>
<td>13</td>
<td>03</td>
</tr>
<tr>
<td>Days</td>
<td>Hours</td>
<td>Min</td>
<td>Sec</td>
</tr>
</table>
I don't have time to write a fiddle for you but here's my suggestion. Write out your html with zero javascript, then take that HTML as a template and write a function like this:
function updateTime(days, hours, min, sec) {
var html = "
<div class='row'>
<div id='days'>"+days+"</div>
<div id='hours'>"+hours+"</div>
..etc
</div>
";
document.getElementById(id).innerHTML = html;
}
Easier to look at and style in my opinion, maybe even try https://mustache.github.io/
Create four separate elements within your #countdown element
<div id="countdown">
<span class="sect" id="days"></span>
<span class="sect" id="hrs"></span>
<span class="sect" id="mins"></span>
<span class="sect" id="secs"></span>
</div>
Then set those elements to display: inline-block
.sect {
display: inline-block;
}
Then set each element's innerHTML separately and include a line-break
days.innerHTML = Math.floor(distance / _day) + '<br> days ';
hrs.innerHTML = Math.floor((distance % _day) / _hour) + '<br> hours ';
mins.innerHTML = Math.floor((distance % _hour) / _minute) + '<br> mins ';
secs.innerHTML = Math.floor((distance % _minute) / _second) + '<br> secs';
CountDownTimer('October 21, 2015 19:00:00', 'countdown');
var days = document.getElementById('days');
var hrs = document.getElementById('hrs');
var mins = document.getElementById('mins');
var secs = document.getElementById('secs');
function CountDownTimer(date, id) {
var end = new Date(date);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
var showRemaining = function() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = 'EXPIRED!';
return;
}
days.innerHTML = Math.floor(distance / _day) + '<br> days ';
hrs.innerHTML = Math.floor((distance % _day) / _hour) + '<br> hours ';
mins.innerHTML = Math.floor((distance % _hour) / _minute) + '<br> mins ';
secs.innerHTML = Math.floor((distance % _minute) / _second) + '<br> secs';
};
timer = setInterval(showRemaining, 1000);
}
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
.sect {
display: inline-block;
}
<div id="countdown">
<span class="sect" id="days"></span>
<span class="sect" id="hrs"></span>
<span class="sect" id="mins"></span>
<span class="sect" id="secs"></span>
</div>
I am trying to figure out why this code is working only in jsbin and not in jsfiddle or in any web browser as an html/js file. I have tried debugging but cannot find a conclusion.
I made the mistake of coding directly in jsbin instead of a document. Any input would be appreciated.
http://jsbin.com/tuduxedohe/7/edit
http://jsfiddle.net/2rs1x5pz/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Timer</title>
<script type="text/javascript" src="part2.js"></script>
</head>
<body>
<h1><div id="time">00:00:00</div></h1>
<div id="result"></div>
<button id="start" onclick ="startClock();" >Start</button>
<button id="stop" onclick="stopTimer();">Stop</button>
<button id="clear" onclick="resetTimer();">Reset</button>
</body>
</html>
var currentTime = document.getElementById('time');
var hundreths = 0;
var seconds = 0;
var minutes = 0;
var t;
function startClock() {
function add() {
hundreths++;
if (hundreths > 99) {
hundreths = 0;
seconds++;
if (seconds > 59) {
seconds = 0;
minutes++;
}
if(minutes >= 10) {
seconds= 0;
minutes= 0;
stopTimer();
}
}
if (hundreths > 9 && seconds < 9) {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + hundreths;
}
else if ((seconds > 9 ) && (hundreths < 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + "0" + hundreths;
}
else if((seconds > 9) && (hundreths > 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + hundreths;
}
else if ((minutes > 9) && (seconds < 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}
else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + "0" + hundreths;
}
else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + hundreths;
}
else {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}
timer();
}
function timer() {
t = setTimeout(add, 1);
}
timer();
} // end function start clock
function stopTimer() {
document.getElementById("result").innerHTML = "<p>" + ("Your time is: " + minutes + " minutes, " + seconds + " seconds, " + "and " + hundreths + " hundreths") + "</p>";
clearTimeout(t);
}
function resetTimer() {
hundreths = 0;
seconds = 0;
minutes = 0;
currentTime.innerHTML = "00:00:00";
}
That is because by default the script is added in a onload handler in jsfiddle, so your methods is available only inside the scope of that closure. So it will be
window.onload=function(){
//your script is here
}
You are trying to access them in global scope when you are trying to call them from on<event>="" attributes which will give an error like Uncaught ReferenceError: startClock is not defined in your console.
Change the second dropdown in the left panel under Frameworks & Extensions to body/head in the left panel of fiddle to add the script without a wrapper function
Demo: Fiddle