I have a setinterval that moves bulldozer from the right to the left.
In the jsfiddle below, the setInterval must stop itself after 5 seconds. (used a settimeout and clearinterval for that) but it's not working. Can anyone help me?
http://jsfiddle.net/B5MKj/11/
var gameover;
gameover = setInterval(function () {
setTimeout(function () {
clearInterval(movingbulldozer);
}, 55000);
}, 10);
You had a typo in your fiddle, updated fiddle, if works just fine, but instead of 5000 ms you had 55000ms set for the timeout.
setTimeout(function () {
clearInterval(movingbulldozer);
}, 5000);
In your example, movingbulldozer is undefined. If you're trying to clear the interval, clear the interval with the right reference. In your example, this would be clearInterval(gameover);
The problem with your example is that every 10 ms you're adding a timeout to the DOM which clears the interval.
var timeout, interval, date,
i = 0;
$(document).ready(function() {
interval = setInterval(function() {
date = new Date();
i++;
$('#debug').html('Interval parsed at '+date.getTime()+', interval #'+i);
if (i >= 100) { // According to your example
$('#debug').html('Starting timeout...');
timeout = setTimeout(function() {
$('#debug').html('Timed out');
}, 5000);
clearInterval(interval);
}
}, 10);
});
Check out my example, see if it helps. :)
http://jsfiddle.net/faqq5/
Related
I'm writing a code to alert the user inactive when the user is idle for some time.
setIdleTimeout = () => {
var timeout = 0;
timeout = setTimeout(onExpires, 500);
//Expires Function
function onExpires() {
timeout = 0;
alert('Timed out');
}
//This function executes on each mouse move.
function onMouseActivity(event) {
clearTimeout(timeout);
timeout = setTimeout(onExpires, 500);
}
}
The above code works fine. But when ever I add a condition to the clearTimeout, Its not working.
example:
//This function executes on each mouse move.
function onMouseActivity(event) {
var mouseSeconds =5
if(mouseSeconds < 10) {
//Now this is not working.
clearTimeout(timeout);
}
timeout = setTimeout(onExpires, 500);
}
Please help me out. This is driving me crazy for hours now.
Thanks.
you wrote
var mouseSeconds =5
if(mouseSeconds > 10) {
//Now this is not working.
clearTimeout(timeout);
}
mouseSeconds is 5 it will never be bigger then 10 :) so the statement is always false.
http://flipclockjs.com/
I need a counter which counts down to zero from a certain time (for example 2 hours). After those two hours the timer needs to reset and starts again.
How can I do this? I can't find anything about it in their docs.
What I got now:
var clock = $('.your-clock').FlipClock({
countdown : true,
});
Try this one:
var clock = $('.your-clock').FlipClock({
countdown : true,
});
clock.setTime(10);
clock.start();
setTimeout(function(){
checktime();
}, 1000);
function checktime(){
t = clock.getTime();
if(t<=0){
clock.setTime(10);
clock.start();
}
setTimeout(function(){
checktime();
}, 1000);
}
You can use method, see 'Chainable Methods' example.
clock.start(function() {
setInterval(function() {
clock.stop().reset().start();
}, 7200);
});
I assume that you want to decrease every second? this is a non jquery way:
var counter, myInterval;
function createCountDown(startHourInSecond){
counter = startHourInSecond;
myInterval =window.setInterval(function(){
counter --;
if (counter ==0){
clearInterval(myInterval);
counter = startHourInSecond;
createCountDown(counter);
}
}, 1000);
}
createCountDown(7200);
If class triggered run another function only once and clear Timeout. I am using Firefox.
Expecting
var i = setInterval(function() {
if $('[style*="display: inline;"]').click();
function start timeout() {
document.querySelector('.body_inside_inside_once_').click()
clearTimeout();
}, 200);
}, 1000);
Your questions is not clear ,and contains syntax error but for rough idea...The following code might help you to clearTimeout upon clicking a HTML element.
var i = setInterval(function() {
function start timeout() {
// Operation
}}, 1000);
document.querySelector('.body_inside_inside_once_').click(function(){
clearInterval(i);
});
This is a followup to this question, where I found out how to make code be repeated every x seconds. Is it possible to make an event that can change this? I.e. I have a checkbox which is meant to control whether this is repeated or not, so I figured I'd need something like this:
$(checkbox).bind("change", function() {
switch(whether if it is ticked or not) {
case [ticked]:
// Make the code repeat, while preserving the ability to stop it repeating
case [unticked]:
// Make the code stop repeating, while preserving the ability to start again
}
});
I have no idea what I could put in the cases.
You can do it by assigning your setInterval function to a variable.
var interval = setInterval(function() { }, 1000);
and then you can stop setInterval by
clearInterval(interval);
p.s.
to start your interval you need to call var interval = setInterval(function() { }, 1000); again
You can either stop and start the interval:
var timer;
function start() {
timer = window.setInterval(function(){
// do something
}, 1000);
}
function stop() {
window.clearInterval(timer);
}
start();
$(checkbox).bind("change", function() {
if ($(this).is(':checked')) {
start();
} else {
stop();
}
});
Or you can have a flag causing the interval to skip the code:
var enabled = true;
var timer = window.setInterval(function(){
if (!enabled) {
// do something
}
}, 1000);
$(checkbox).bind("change", function() {
enabled = $(this).is(':checked');
});
function fooFunc() {
$('#foo').text(+new Date());
}
var id;
var shouldBeStopped = false;
$('input').change(function() {
if (shouldBeStopped)
clearInterval(id);
else
id = setInterval(fooFunc, 200);
shouldBeStopped = !shouldBeStopped;
});
Live DEMO
how would you guys reinitiate a javascript timer after it's been stopped. Basically I need the timer to stop, and then start again on rollout of a div. I have
var timer = setInterval(myFunction, 1000);
timer
function myFunction(){<br/>
if(){
do this
}
else if () {
do this
}
};
then I have
$("td").hover(
function () {
window.clearInterval()
},<br/>
function () {
timer
}
);
all works good except it doesn't start again on hover off. If I put a new timer in hover off the timer starts to speed up every time you hover on and off.... Any ideas.
Thanks
var intervalID = setInterval(myFunction, 1000);
function myFunction(){
//logic goes here
};
$("td").hover( function () {
window.clearInterval(intervalID)
},
function () {
intervalID = setInterval(myFunction, 1000);
} );
Explanation: What's happening is that the setInterval is returning the ID associated with that interval.
I did a littse jsFiddle to show it works.
NOTE: this doesn't pause the current Interval. It stops it and starts it again. So if you were in a 10sec interval at 9 secs and stopped it. When it starts again it will take another 10secs.