clearInterval not working from interval inside setTimeout - javascript

Clear interval is not working for an interval i use inside setTimeout().
var $scroller;
setTimeout(function() {
var div = jQuery('.module-content-slider');
$scroller = setInterval(function(){
/* interval function */
}, 25)
}, 1000);
clearInterval($scroller);

The clearInterval runs before setInterval within the "timeout" after 1 second (1000 milliseconds) is executed — effectively clearing an unset $scroller interval.

There is no what to clear.
The interval doesn't exist at clearInterval($scroller); run time.
clearInterval will be executed a second later.

Looks like your use case is to trigger interval function for every 25 ms and clear the interval after 1000 ms.
If thats the scenario then it should be
var $scroller = setInterval(function(){
/* interval function */
}, 25);
setTimeout(function() {
var div = jQuery('.module-content-slider');
clearInterval($scroller);
}, 1000);

Related

How do i make a javascript fuction repeat every few seconds? [duplicate]

I want repeat this code every 4 seconds, how i can do it with javascript or jquery easly ? Thanks. :)
$.get("request2.php", function(vystup){
if (vystup !== ""){
$("#prompt").html(vystup);
$("#prompt").animate({"top": "+=25px"}, 500).delay(2000).animate({"top": "-=25px"}, 500).delay(500).html("");
}
});
Use setInterval function
setInterval( fn , miliseconds )
From MDC docs:
Summary
Calls a function repeatedly, with a fixed time delay between each call to that function.
Syntax
var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);
where
intervalID is a unique interval ID you can pass to clearInterval().
func is the function you want to be called repeatedly.
code in the alternate syntax, is a string of code you want to be executed repeatedly. (Using this syntax is not recommended for the same reasons as using eval())
delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. As with setTimeout, there is a minimum delay enforced.
Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer.
Example
// alerts "Hey" every second
setInterval(function() { alert("Hey"); }, 1000);
setInterval(function(){
// your code...
}, 4000);
It's not too hard in javascript.
// declare your variable for the setInterval so that you can clear it later
var myInterval;
// set your interval
myInterval = setInterval(whichFunction,4000);
whichFunction{
// function code goes here
}
// this code clears your interval (myInterval)
window.clearInterval(myInterval);
Hope this helps!
Another possibility is to use setTimeout, but place it along with your code in a function that gets called recursively in the callback to the $.get() request.
This will ensure that the requests are a minimum of 4 seconds apart since the next request will not begin until the previous response was received.
// v--------place your code in a function
function get_request() {
$.get("request2.php", function(vystup){
if (vystup !== ""){
$("#prompt").html(vystup)
.animate({"top": "+=25px"}, 500)
.delay(2000)
.animate({"top": "-=25px"}, 500)
.delay(500)
.html("");
}
setTimeout( get_request, 4000 ); // <-- when you ge a response, call it
// again after a 4 second delay
});
}
get_request(); // <-- start it off
const milliseconds = 4000
setInterval(
() => {
// self executing repeated code below
}, milliseconds);
Call a Javascript function every 2 second continuously for 20 second.
var intervalPromise;
$scope.startTimer = function(fn, delay, timeoutTime) {
intervalPromise = $interval(function() {
fn();
var currentTime = new Date().getTime() - $scope.startTime;
if (currentTime > timeoutTime){
$interval.cancel(intervalPromise);
}
}, delay);
};
$scope.startTimer(hello, 2000, 10000);
hello(){
console.log("hello");
}

clearInterval() what happens with the last interval call

var interval = setInterval(function()
{ function }, 1000);
clearInterval(interval);
What happens with the last interval call when clearInterval is called? If the interval is 500 out of 1000 (.5 seconds) into the interval and clearInterval is called, does this function get called and then the interval is cancelled? Or does the function not get called and the interval is cancelled?
Thanks
If the specified interval hasn't yet elapsed, the callback will not be invoked again. The standard isn't clear on what would happen if the interval has elapsed but the callback has not yet been processed by the event queue, but in the case where there are 500 ms remaining before the next invocation, it definitely shouldn't invoke the callback again.
You can test it out with something like:
var counter = 0;
var interval = setInterval(function() {
console.log(counter);
counter++;
}, 2000);
setTimeout(function() {
clearInterval(interval);
}, 5900);
=> 0
=> 1
As you can see, if the interval is interrupted midway, the callback won't be executed.

How would I make a Timer which times things in 0.75 seconds Javascript?

Just wondering how I would make a timer which times in 0.75 seconds instead of an entire second. I already know how to make a 1 second timer.
var myTimer = setInterval(function(){
console.log("This will appear every second");
}, 1000);
Thanks
The second parameter of the setInterval function is the interval time in milliseconds - the 1000. Just change it to 750.
var myTimer = setInterval(function() {
console.log("This will appear every 750 milliseconds");
}, 750);
Use 750 as value for the timeout delay
setInterval(/*...*/, 750);
Regards

js setInterval - increase speed gradually

How can I make setInterval to increase speed gradually, like start from 1000 ms and than go down until 40 ms gradually in few seconds by 1 ms at a time.
Any thoughts?
This is my code:
setTimeout(function() {bonustimer = setInterval(function() { handleTimer2(rushcount); }, 40);}, 1000);
handleTimer2 = function() {
if(rushcount === -1) {
clearInterval(bonustimer);
} else {
$('.digit-wrapper').html(rushcount);
rushcount--;
}}
Set interval probably wouldn't be what you want here, as you just end up killing it and redoing it on every iteration. Much easier to use setTimeout, possibly something like this:
(function () {
var interval = 1001;
timer = function() {
--interval;
//do your thing here
if (interval >= 40) {
setTimeout(timer, interval);
}
};
timer();
})();
Also note that if you only decrease the interval by one ms at a time, from 1000 down to 40, it takes quite a while to go through all those iterations. You can always replace --interval by some other formula, like interval = interval*0.9; (to reduce by 10% each iteration) or whatever formula you want.
I guess the code by #OldGeeksGuide just stops when the interval reaches 40. Needs a little improvement ;)
(function () {
var interval = 1000;
timer = function() {
interval--;
//do your thing here
interval = interval < 40 ? 40 : interval;
setTimeout(timer, interval);
};
timer();
})();
Then you could maybe need to stop this loop by wrapping the setTimeout with some if condition ;)

Change millisec for window.setInterval function

I used window.setInterval function. this function includes 3 arguments :
setInterval(code,millisec,lang)
I used that like this:
var counter = 1;
window.setInterval(function() {}, 1000 * ++counter);
but when first time set timer (second argument), is not changed and that act Like below code:
window.setInterval(function() {}, 1000);
please write correct code for change timer
Use window.setTimeout instead.
var delay = 1000;
function myTimer() {
// do whatever
window.setTimeout(myTimer, delay);
}
window.setTimeout(myTimer, delay);
You can manipulate delay in the body of your function.
Your problem is that javascript first execute '1000 * ++counter' once and then do not update the time interval.
You should try to use a timeout instead: https://developer.mozilla.org/en/DOM/window.setTimeout
And create a new time out with the new value every time your time out function is called.
Sounds like what you're after is not setInterval but rather setTimeout in a loop:
var counter = 1;
for (var i = 1; i <= 3; i++) {
window.setTimeout(function() {
alert("#" + counter);
counter++;
}, i * 1000);
}
This will execute three different "timers" one after the other.
Live test case: http://jsfiddle.net/86DRd/

Categories

Resources