I have developed the below code, which creates a div pop up box for 1 minute and disappears for 9 minutes. The div pop up box appears every 10 minutes on the hour based on the time on your device and it continuous.
function showPopup() {
var now = new Date();
if ((now.getMinutes() % 10) == 0) {
document.getElementById("popup").style.display = "block";
setTimeout(function() {
document.getElementById("popup").style.display = "none";
}, 60 * 1000); // Display for 1 minute
} else {
document.getElementById("popup").style.display = "none";
setTimeout(showPopup, 30 * 1000); // Rteyr every 30 seconds
}
}
showPopup();
.outer {
width: 500px;
height: 500px;
margin: auto;
background-color: red;
}
.inner {
width: 200px;
height: 100px;
margin: auto;
padding: auto;
color: red;
background-color: white;
display: none;
}
<div class="outer">
<div id="popup" class="inner">
This is the pop-up.
</div>
</div>
I am looking to create a piece of text to show the time in minutes the length to wait, when the div pop up box is not appearing, until the div pop up box appears, so:
“9 minutes to wait”
“8 minutes to wait”
“7 minutes to wait”
“6 minutes to wait”
“5 minutes to wait”
“4 minutes to wait”
“3 minutes to wait“
“2 minutes to wait”
“1 minute to wait”
With only the minutes counting down and with the “1 minute” without an “s” at the end. Would this be possible using the same code?
I think this should work for you. Feel free to ask if in case of having any doubt :)
function showPopup() {
var now = new Date();
if ((now.getMinutes() % 10) == 0) {
document.getElementById("popup").style.display = "block";
document.getElementById("popup").innerHTML = 'This is the pop-up.'
setTimeout(function() {
//document.getElementById("popup").style.display = "none";
document.getElementById("popup").innerHTML = '9 Minutes to wait.'
}, 60 * 1000); // Display for 1 minute
} else {
var x = 10 - now.getMinutes()%10
document.getElementById("popup").style.display = "block";
if (x == 1){
document.getElementById("popup").innerHTML = x + ' '+ 'Minute to wait.'
}
else{
document.getElementById("popup").innerHTML = x + ' '+ 'Minutes to wait.'
}
setTimeout(showPopup, 30 * 1000); // Rteyr every 30 seconds
}}
showPopup();
Related
I have been developing a pop up div to show up after 60 seconds and I have had help with a kind person on here last week to help finish the code, as shown below:
const startTime = new Date(),
popup = document.getElementById("popup"),
textPad = document.getElementById("text");
function showPopup() {
const now = new Date(new Date() - startTime),
minsLeft = now.getSeconds() % 60;
if (minsLeft > 58) {
popup.style.display = "inline-block";
textPad.style.display = "none";
setTimeout(showPopup, 60 * 1000); // Keeps message
} else {
popup.style.display = "none";
textPad.style.display = "block";
textPad.textContent = `${59 - minsLeft} second${minsLeft - 58 ? 's' : ''}`;
setTimeout(showPopup, 1 * 1000); // Countdown
}
}
showPopup();
.outer {
width: 500px;
height: 500px;
margin: auto;
background-color: red;
}
.inner {
width: 200px;
height: 100px;
margin: auto;
padding: auto;
color: red;
background-color: white;
display: none;
}
<div class="outer">
<div id="popup" class="inner">
This is the pop-up.
</div>
<div id="text"></div>
</div>
I just wonder if any one would know how to make the red text on a white background ‘This is a pop up’ pop up div remain on the screen and not disappear after the 60 seconds?
const startTime = new Date(),
popup = document.getElementById("popup"),
textPad = document.getElementById("text");
function showPopup() {
const now = new Date(new Date() - startTime),
minsLeft = now.getSeconds() % 10;
if (minsLeft > 58) {
popup.style.display = "block";
textPad.style.display = "none";
} else {
textPad.textContent = `${9 - minsLeft} minute${minsLeft - 8 ? 's' : ''} to wait`;
setTimeout(showPopup, 1 * 1000); // Redo on every minute
}
}
showPopup();
This any good?
I need a survey to auto progress to the next block when a specified time limit has been reached for the previous block. Currently I am using the below script to display the timer but need the script to execute the action when time has elapsed. Any assistance is greatly appreciated.
Javascript in timing question.
Qualtrics.SurveyEngine.addOnload(function()
{
var headerCont = document.createElement("div");
headerCont.className = "header-cont";
headerCont.id = "header_container";
var header = document.createElement("div");
header.className = "header"
header.id = "header_1";
var timer = document.createElement("div");
timer.className = "timer";
timer.id = "timer_1";
timer.innerHTML = "Time Remaining: <span id='time'>00:10</span>";
headerCont.appendChild(header);
header.appendChild(timer);
document.body.insertBefore(headerCont, document.body.firstChild);
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
var myTimer = setInterval(function() {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
var text = ('innerText' in display)? 'innerText' : 'textContent';
display[text] = minutes + ":" + seconds;
if (--timer < 0) {
clearInterval(myTimer);
timeOver();
}
}, 1000);
}
var timerSeconds = 10,
display = document.querySelector('#time');
startTimer(timerSeconds, display);
var timeOver = function() {
document.getElementById("timer_1").innerHTML = "Time is up.";}
});
CSS
.header-cont {
width:100%;
position:fixed;
top:0px;
z-index:1000;
}
.header {
height:75px;
background:#FFFFFF;
width:100%;
margin:0px auto;
}
.timer{
margin: auto;
text-align: center;
vertical-align: middle;
font-size: 200%;
font-family: Arial;
}
EDITED ANSWER BASED ON COMMENT:
Initialize an embedded data field in the survey flow before block:
blockTimeFlag = 0
Add the following lines to the timeOver function:
Qualtrics.SurveyEngine.setEmbeddedData("blockTimeFlag", "1");
$('NextButton').click();
However, this means that none of your questions can be forced response.
Add display logic to your questions:
if blockTimeFlag = 0
This is my code for displaying countdown time for 1 minute on a HTML page:
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()',1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
setTimeout('Decrement()',1000);
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}
</script>
</head>
<body>
<input id="minutes" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight:bold;">m
<input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;">s
</body></html>
I want to show an alert message displaying "Time over" when the countdown reached to 0m 0s and when the user click on ok button of the alert box, the page should be redirected to example.html. How can I achieve this?
Edit : I have done this way-
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
if (minutes==0 && seconds==0){
if (window.confirm('Time over'))
{
window.location="http://www.example.com";
}
else
{
window.location="http://www.example.com";
}
}
secs--;
setTimeout('Decrement()',1000);
}
But it is not working. The counter goes negative instead of asking for conformation at 0m 0s. How to make it working?
If your timeout has exceeded you can call the confirm Function like this:
if (window.confirm('The countdown has ended. Redirect?'))
{
// They clicked Yes
}
else
{
// They clicked no
}
The further possibilities with this function are described here:
Confirm
So before you call the setTimeout() again in your Decrement function you can check if the minutes and seconds are zero and then call the confirm action. Otherwise, if this condition is not met you proceed with decrementing.
Edit
You also had some errors in your JavaScript Code. I´ve done some improvements on it and you can look at it at this JsFiddle:
Countdown
First of all you´ve had a problem, because you´ve never set the minutes and the seconds back to the inputs. Next, you hadn´t closed the Tags for the minutes and the seconds. I guess the setTimeout didn´t work either, because of the passed string. Often it´s better to use a full function expression in those cases. You can see the changes in the JsFiddle.
How about using setInterval()? Like the following:
var timeCount = setInterval(Decrement, 1000);
function Decrement() {
if (secs === 0) {
clearInterval(timeCount);
return;
}
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
}
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 0;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = 3;
var minutes, seconds;
function countdown() {
setTimeout('Decrement()',1000);
}
function Decrement() {
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
console.log(minutes + ', ' + seconds);
if (seconds.value <= 0 && minutes.value <= 0) {
if (window.confirm('timeout')) {
alert('done');
} else {
alert('nothing to do');
}
} else {
setTimeout('Decrement()',1000);
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}
// It's necessary
document.onreadystatechange = function() {
if(document.readyState == "complete") {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
countdown();
}
}
</script>
</head>
<body>
<input id="minutes" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight:bold;">m
<input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;">s
</body>
</html>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Im trying to make a countdown timer that would change the color when reaches two different points, it supposed to go orange when reaches "00:59" and then change to red when reaches " 00:00 " How do I do that with javascript.
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> : <input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
<script>
countdown();
</script>
It's very easy, you just need to add a condition before inserting value in both div or minutes/seconds.
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
function colorchange(minutes, seconds)
{
if(minutes.value =="00" && seconds.value =="59")
{
minutes.style.color="orange";
seconds.style.color="orange";
}
else if(minutes.value =="00" && seconds.value =="00")
{
minutes.style.color="red";
seconds.style.color="red";
}
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
colorchange(minutes,seconds);
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> :
<input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
<script>
countdown();
</script>
Here you are. I use a colors array, store currentColorIndex, then reset index if it > colors.length. You can improve this by create random number for currentColorIndex, avoid my boring loop.
var mins = 1;
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
// THE MAGIC BEGIN HERE
var colors = ["red", "green", "blue", "cyan", "magenta", "yellow", "black"];
var currentColorIndex = 0;
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.style.color = colors[currentColorIndex];
seconds.style.color = colors[currentColorIndex];
minutes.value = getminutes();
seconds.value = getseconds();
if (++currentColorIndex > colors.length) currentColorIndex = 0;
}
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
countdown();
This is only valid for the next
<input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">:
<input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
Hope this helps.
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
document.getElementById('timer').style.backgroundColor = '#f08000';
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
if (secs < 0) {
document.getElementById('timer').style.backgroundColor = '#ff0000';
clearTimeout(timeout);
return;
}
countdown();
When the timer reaches a certain point (as already in the if statements), change the color of the timer div using the DOM.
I was trying to make a countdown timer that once it reaches " 00:00 " it should go up again without limit.
I can't figure it out how to make my countdown go up once it reaches " 00:00 " maybe you can help me.
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 1;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
var timeout;
function countdown() {
timeout = setTimeout('Decrement()', 1000);
}
function colorchange(minutes, seconds)
{
if(minutes.value =="00" && seconds.value =="59")
{
minutes.style.color="orange";
seconds.style.color="orange";
}
else if(minutes.value =="00" && seconds.value =="30")
{
minutes.style.color="red";
seconds.style.color="red";
}
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
colorchange(minutes,seconds);
secs--;
if (secs < 0) {
clearTimeout(timeout);
return;
}
countdown();
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 110px; border: none; background-color:none; font-size: 100px; font-weight: bold;"> :
<input id="seconds" type="text" style="width: 110px; border: none; background-color:none; font-size: 100px; font-weight: bold;">
</div>
<script>
countdown();
</script>
I think your current solution is a little overcomplicated. You have a function that sets a timeout that calls another function which does the work and then re-calls the function that sets the timeout again.
Instead of doing that, just use the setInterval method instead.
Similarly to what #JoColina suggested, set a direction variable that indicates which direction to count, and then set up different behavior for counting up vs. counting down.
var direction = 'down';
var mins = 1.1;
var secs = mins * 60;
function colorchange() {
var className;
if (direction == 'up') {
className = 'success';
} else if (secs <= 30) {
className = 'danger';
} else if (secs <= 59) {
className = 'warning';
}
document.getElementById('timeText').className = className;
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}
function countdown() {
setInterval(function() {
var minutes = document.getElementById('minutes');
var seconds = document.getElementById('seconds');
minutes.value = getminutes();
seconds.value = getseconds();
colorchange();
if (direction == 'down') {
secs--;
if (secs <= 0) {
direction = 'up';
}
} else if (direction == 'up') {
secs++;
}
}, 1000);
}
countdown();
.success,
.success input {
color: green;
}
.warning,
.warning input {
color: orange;
}
.danger,
.danger input {
color: red;
}
<div id="timer">
This is only valid for the next
<span id="timeText">
<input id="minutes" type="text" style="width: 110px; border: none; background-color:none; font-size: 100px; font-weight: bold;">:
<input id="seconds" type="text" style="width: 110px; border: none; background-color:none; font-size: 100px; font-weight: bold;">
</span>
</div>
tl;dr, however you could declare a global boolean like var down = true;, and once your timer reaches 00:00, you just change down = true to down = false.
Then, on the function that changes the counter you add:
if(down){
Decrement();
}else{
Increment():
}
And if, for example, you want to decrement again once it reaches 13:54 you once more add a down = true.
Hope this helps!