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
Related
This question already has answers here:
Getting a random value from a JavaScript array
(28 answers)
Closed 6 months ago.
I would like to make something where I have a bank of phrases or questions and every few seconds or every time the page is refreshed a new message is shown.
Any idea how to do this?
setInterval(() => {
alert("My message");
}, 1000);
Where you replace 1000 with the number of milliseconds you want the message to repeat. 1000 milliseconds = 1 second.
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.
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';
This question already has answers here:
Call a Javascript function every 5 seconds continuously [duplicate]
(5 answers)
Closed 8 years ago.
I am new to Greasemonkey and javascript but have found the script below, I want to execute it every 5 seconds.
javascript:$('.comments-stream .more:not(.dnone)').parent().find('.fa-minus').parent() .click();
Is there any way of doing this?
Use setInterval. Notice how i removed the parentheses from the click function.
javascript:setInterval($('.comments-stream .more:not(.dnone)').parent().find('.fa-minus').parent() .click, 5000);
If you want to do more then just the click.
javascript:setInterval(function(){
$('.comments-stream .more:not(.dnone)').parent().find('.fa-minus').parent() .click();
, 5000);
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.