This question already has answers here:
How to write a countdown timer in JavaScript? [closed]
(3 answers)
Closed 6 years ago.
I've written up a Trivia Game in Javascript, but am having a hard time understanding how to correctly implement a timer for each question. I've made it so that each question gets presented individually, and I'd like to have a timer for each question. If the timer runs out, it should proceed to the next question.
Here is a link to JSFiddle
If someone could take a few minutes and modify the JSFiddle, or let me know what I'd have to do in order to make each question count down from 10, I'd greatly appreciate it. Thanks a lot!
Timers in JavaScript work asynchronously. This is the first thing you should know. Secondly depending on what you need you can use either a:
setTimeout(function, timeout) This one allows you to delay an execution of a function provided by a time provided (in milliseconds).
setInterval(function, timer) This one makes the function call every timer milliseconds
Depending on how you intertwine these your code should do something like:
function timerExpired(){
questionCounter++;
newQuestion();
setTimeout(timerExpired, 15000);
}
//This one will make sure that every 15 seconds, your questions get "moved on" to the next question. You can do the same with an interval, like so:
setInterval(function(){
questionCounter++;
newQuestion();
}, 15000);
This is as far as I can go without this turning into me writting your code.
Related
This question already has answers here:
Redirect website after specified amount of time
(7 answers)
Closed 6 years ago.
I was just wondering how I could make a person stay on a website for a specific time and then redirect them to a new website using JavaScript!
Thanks for your help!
I repeat, using JavaScript, this is not the same as the other ones, I want to use JS
Will take you to Google after 3 seconds.
var timer = setTimeout(function() { window.location.href = 'http://www.google.com'; }, 3000);
Please always set the timeout to variable for the possibility to clear the timeout.
clearTimeout(timer);
setTimeout(myFunction, 2000);
myFunction should use window.location.href = 'my_Link';
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't know how to make a Javascript count up that is related to the real time, which means when you reload the page, the counter won't start over again. Would anybody tell me how to make that happen :) Example like http://www.worldometers.info/ Thanks a lot.
The code they are using is likely pulling from a database with an actual value increasing live.
Check out this js fiddle I made for an example of how a simple timer can work. Notice that if you press "Run" multiple times, the time itself will stay constant.
Using a remote database will cause a lot more work, but for saving values across browser refreshes you should learn about localStorage I'd check out W3 School's article on the subject.
In my implementation I use
localStorage.setItem("time", currentTime); // Note that CurrentTime must be a string!
in each iteration of your code after setting the currentTime var.
When you start up your application, a simple if statement
if (localStorage.getItem("time") {
CurrentTime = localStorage.getItem("time");
} else {
// set default values
}
will work, as localStorage.getItem will return null if the value doesn't exist (or if you set the value to null manually).
(for localStorage, you can also use [bracket notation] and will probably see that in most common examples)
localStorage["foo"] = "value";
// is the same as:
localStorage.setItem("foo", "value")
// and
var bar = localStorage["foo"];
// is the same as:
var bar = localStorage.getItem("foo");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to activate an audio file every round 15 minutes using Date Object.
I have tried to use a while loop to determine the specific time but it's not working.
Check out setInterval()
Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.
and setTimeout().
Calls a function or executes a code snippet after a specified delay.
I'd imagine you'll use these in conjunction with an <audio> element for playback.
A quick solution would be something like this (fiddle):
(function () {
var time = new Date().getTime();
var interval = 900000;
function playAudio() {
document.getElementById('audio-test').play();
}
setTimeout(function () {
playAudio();
setInterval(playAudio, interval); // execute on interval
}, interval - time % interval); // execute at next interval
})();
Perform a setTimeout to catch up to the next interval and then use setInterval after that. One caveat to this approach is that the delay specified for these functions is a minimum delay, i.e.: the actual delay may be longer. So, the interval could drift out of accuracy.
Alternatively, you could poll at a shorter interval and check that the minutes are evenly divided by 15. Here's a fiddle
(function () {
var last;
setInterval(function(){
var minutes = new Date().getMinutes();
if(minutes % 15 === 0 && last !== minutes){
last = minutes;
document.getElementById('audio-test').play();
}
},1000);
})();
This question already has answers here:
Javascript Countdown
(6 answers)
Closed 9 years ago.
I am trying get a counter to work in PHP so it basically counts '5' then '4' and so on till 0. It will redirect to a different page. I was just wondering how this can be done in PHP? if possible, if not what it would be in javascript?
Take a look at this. Other than that … you should really try to do some original research yourself and ask meaningful questions if you are stuck by illustrating your problem and showing us your current solution/approach.
The following code will redirect after 5 seconds:
<script>
setTimeout(function () {
window.location.href = "newpage.html";
}, 5000); //seconds
</script>
here's a example of count down timer: link
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I clearInterval() for all setInterval()?
I called setInterval in my page, and I don't have access to the original variable that setInterval was stored in, but I want to clear it from my page.
so how can i clear all timers present on a page?
Clear ALL the intervals!
// This could take a while.
for (var i=0; i<2147483647; i++) clearInterval(i);
You could probably tone down that max value of i and still catch 'em all. The above code finished in my browser after only 12008757 milliseconds. That's 3.3 hours, or ~179 clearInterval()s per millisecond.