yet another clearInterval in javascript not working - javascript

Belive me i ve seen many solutions i still dont know why this doesnt work.
timerId = 0;
$("#dumpStartId").click(function(){
var proId = $("#curProfileId").val();
timerId = setInterval(function(){
showact();
showactdat();
},1000);
});
$("#dumpStopId").click(function(){
clearInterval(timerId);
document.getElementById('curSeqId').value = "";
if(timerId)
{
clearInterval(timerId);
}
});
dumpstartid and dumpstopid are the ids of the two buttons. Even after clicking stop the intervals are executing continuosly. Im not sure whats wrong with this code. No errors in the console too.
Update: I can observer that sometimes after insanely pressing the button for around 5 or 6 times it comes to a complete halt. Cant this thing stop instantaneously (or is too much to ask??)

setInterval iterates every "Millisecond-Value" you've given till it reaches the clearInterval. Try using onmousedown instead of onclick.

It was because the function the setInterval is calling was problematic which polls a rest service at a varied interval from the setIntervals interval causing it to conflict. I ve corrected it and that fixed it.

Related

clearInterval not working on shuffle function

My clearInterval function is not working from a toggle button that I have created.
$('button.shuffle').click(function() {
if(isShuffling) {
clearInterval(discoLights);
isShuffling = false;
console.log('stop shuffling')
} else {
disco = setInterval(discoLights ,3000);
isShuffling = true;
console.log('start shuffling')
}
});
I am aware that this kind of question has been asked numerous times. I did a lot of research on it within SO and I could not find solution to my problem.
Here is the JSFiddle of the code I have been working on: https://jsfiddle.net/coolwebs/zLgsdno7/7/
I have tried reordering the code on several occasions, but nothing is working. I have used setInterval and clearInterval before on page load (not set with buttons) and I have not had an issue before...
Use
clearInterval(disco);
instead of
clearInterval(discoLights);
As clearInterval takes the id which is returned by setInterval
Issue is in clearInterval().
It should be clearInterval(disco) instead of
clearInterval(dicoLights).
See clearInterval

Meteor setinterval too fast

I have multiple countdown timers in my app and I'm updating those values using setInterval function. The idea of following code block is the loop through all ongoing games and update countdown clocks. I have 1000ms interval but it loops much faster than that. Why? When I open multiple browser windows to see countdown timer updates I can notice that interval is something like 500ms.
if (Meteor.isClient) {
Meteor.setInterval(
function() {
var going = Games.find({isGoing: true});
var goingFetch = going.fetch();
var goingCount = going.count();
for (i = 0; i < goingCount; i++) {
Games.update(goingFetch[i]._id, {$set: {timeLeft: goingFetch[i].timeLeft - 1}});
}
}
, 1000 );
}
Is there better way to solve this problem? I may have 100 games simultaneously and every game have one to many timers. Timer type can be countdown or incremental and all timers are stoppable. Interval is always and in all cases 1000ms.
UPDATE
I did more tests and setInterval works fine but clients still have too fast countdown timer.
Are you sure your code isn't executed twice by mistake? Does goingCount produce correct results, for instance? setInterval isn't as accurate as setTimeout, but it shouldn't cause problems at 1000ms. Can we see more of your code?

Javascript background running code

Is there some sort of window.onThis(); function that I can set so that my code runs in a loop in the background?
window.onload = function() {while(true) {console.log("Blah")}}
This makes the page unresponsive. What is the recommended method of doing this?
I feel something's going over my head. Perhaps I'm looking at it the wrong way.
Javascript can only run one thread at a time, so when it is running console.log ("Blah") forever as fast as possible, it is unable to do anything else.
A better way would be to use setInterval, e.g.
var a = setInterval(function () { console.log("blah"); }, 1000);
// Set the function to be called every 1000 milliseconds
//(optional) some time later
clearInterval(a);
// Stop the function from being called every second.
In general, a busy infinite loop ( while (true) { ... } ) is never a good idea.
See https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval
It makes the page unresponsive because you've created an infinite loop. The while loop condition will always be true therefore the loop will never stop running.
I think you are looking for setInterval(), see here https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval

setInterval stops without any reason when browser tab is background

I am working on an application that sends current timestamp to database every 2 minutes with AJAX using setInterval.
But somehow setInterval stops after some minutes (i didnt calculate the exact time), but i believe it happens when i dont open that browser's tab for 20-30 minutes.
function tmstmp() {
$.post("send_functions.php?act=time");
}
$(function() {
setInterval(tmstmp, 60000);
});
Is that normal that setInterval stops if that tab is not on foreground ?
If yes, how can i prevent setInterval to stop ? or check if it stopped ?
Thanks
You should try to make an function call on page startup:
test();
and then loop that function:
function test() {
setTimeout(function() {
// your code
test();
}, 2000);
}
That's not supposed to happen.
Browsers may indeed reduce the timer resolution to something around 1/s, but not clear the setInterval.
Could there be some bug in your code that causes a clearInterval?
No code + no debug information = hard to tell what went wrong.
Just to be sure add the following line to the code (method) that gets executed with setInterval and watch after 20-30 minutes if you still get output in the console.
console.log('Yep! I am alive!');
EDIT: Could be anything but try changing the tmstmp method to include a callback function after the POST request gets executed. That way you'll at least know that it works.
function tmstmp() {
$.post("send_functions.php?act=time", function(data){
console.log('Yep! I am alive!');
});
}

Is it OK to call clearInterval inside a setInterval handler?

I have a piece of Javascript that checks for a condition (via an AJAX call) every n seconds. If that condition is true, it stops checking. I have implemented it in the following way:
var stopTimer;
var timerId = setInterval(function() {
/* Make Ajax Calls and set stopTimer */
if (stopTimer) {
clearInterval(timerId);
}
}, 10000);
However, I find erratic behaviour: Works sometimes, but at other times, it keeps checking forever. I have checked that (as much as is possible) there is no error in any part of the code.
I am therefore suspecting that calling clearInterval inside a setInterval handler might be the culprit. Is that right? Is it OK to call clearInterval inside a setInterval handler?
Thank you for your attention
It's safe. The issue is probably to do with stopTimer not being set as you expect.
I don't think there will be any issue with your code unless the AJAX function is erroneous. You have to take care of the success and error callbacks of the AJAX function so that there won't be any issue with the loop not being stopped.
Also I think you are constantly polling the server for a response and then doing the appropriate action. You can use Reverse AJAX to do this kind of process.
Make sure you're not inadvertently re-using the same timer name elsewhere in your code which would result in you always stopping the second timer to be defined.
Either give the timer a unique name, or scope it to a function
var timerForAjax = setInterval(function() {
/* Make Ajax Calls and set stopTimer */
if (stopTimer)
{
clearInterval(timerForAjax);
}
}, 10000);
I was careless enough to call my timer interval and didn't realize I was creating two timers in the same scope both called interval. Blamed iOS8 for about an hour until I realized that that was nothing to do with it.

Categories