Is JavaScript clearInterval asynchronous?












0














I have the code below.



var intervalId;

function myCallback() {
$.ajax({
url: "http://bla.html",
cache: false,
success: function(data) {
if (intervalId) {
clearInterval(intervalId);
}

if (data && data.result) {
return;
}

alert(data.result);
}
, timout: 2000
});
}

function callInterval() {
intervalId = setInterval(myCallback, 5000);
}

callInterval();


The problem is that the "clearInterval(intervalId)" doesn't seems to invalidate the interval at this right time because the message alert is shown twice.



So here is my question, is clearInterval(intervalId) asynchronous?










share|improve this question
























  • The AJAX request is probably taking more than a second. Why aren't you using setTimeout?
    – SLaks
    Dec 27 '18 at 16:26










  • Actually, I'm using setTimeout. I didn't put it in the question. My interval executes every 5 seconds, and I've used a timeout of 2 seconds.
    – Rhuan Karlus
    Dec 27 '18 at 16:35












  • What are you trying to do? I mean, what is the goal? it doesn't really make much sense to use an interval to repeat an http call. I guess the goal is to execute an http call every X seconds, as long as one is not running already, but this code doesn't really serve this purpose.
    – briosheje
    Dec 27 '18 at 16:36










  • the message is shown twice because you clear the interval when you get the response not when you start the request. This means that during the latency since the first response, the interval is still starting ajax requests
    – quirimmo
    Dec 27 '18 at 16:37










  • Sorry guys, I've edited the code. Now you can help me?
    – Rhuan Karlus
    Dec 27 '18 at 16:39


















0














I have the code below.



var intervalId;

function myCallback() {
$.ajax({
url: "http://bla.html",
cache: false,
success: function(data) {
if (intervalId) {
clearInterval(intervalId);
}

if (data && data.result) {
return;
}

alert(data.result);
}
, timout: 2000
});
}

function callInterval() {
intervalId = setInterval(myCallback, 5000);
}

callInterval();


The problem is that the "clearInterval(intervalId)" doesn't seems to invalidate the interval at this right time because the message alert is shown twice.



So here is my question, is clearInterval(intervalId) asynchronous?










share|improve this question
























  • The AJAX request is probably taking more than a second. Why aren't you using setTimeout?
    – SLaks
    Dec 27 '18 at 16:26










  • Actually, I'm using setTimeout. I didn't put it in the question. My interval executes every 5 seconds, and I've used a timeout of 2 seconds.
    – Rhuan Karlus
    Dec 27 '18 at 16:35












  • What are you trying to do? I mean, what is the goal? it doesn't really make much sense to use an interval to repeat an http call. I guess the goal is to execute an http call every X seconds, as long as one is not running already, but this code doesn't really serve this purpose.
    – briosheje
    Dec 27 '18 at 16:36










  • the message is shown twice because you clear the interval when you get the response not when you start the request. This means that during the latency since the first response, the interval is still starting ajax requests
    – quirimmo
    Dec 27 '18 at 16:37










  • Sorry guys, I've edited the code. Now you can help me?
    – Rhuan Karlus
    Dec 27 '18 at 16:39
















0












0








0







I have the code below.



var intervalId;

function myCallback() {
$.ajax({
url: "http://bla.html",
cache: false,
success: function(data) {
if (intervalId) {
clearInterval(intervalId);
}

if (data && data.result) {
return;
}

alert(data.result);
}
, timout: 2000
});
}

function callInterval() {
intervalId = setInterval(myCallback, 5000);
}

callInterval();


The problem is that the "clearInterval(intervalId)" doesn't seems to invalidate the interval at this right time because the message alert is shown twice.



So here is my question, is clearInterval(intervalId) asynchronous?










share|improve this question















I have the code below.



var intervalId;

function myCallback() {
$.ajax({
url: "http://bla.html",
cache: false,
success: function(data) {
if (intervalId) {
clearInterval(intervalId);
}

if (data && data.result) {
return;
}

alert(data.result);
}
, timout: 2000
});
}

function callInterval() {
intervalId = setInterval(myCallback, 5000);
}

callInterval();


The problem is that the "clearInterval(intervalId)" doesn't seems to invalidate the interval at this right time because the message alert is shown twice.



So here is my question, is clearInterval(intervalId) asynchronous?







javascript asynchronous synchronization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 16:37

























asked Dec 27 '18 at 16:25









Rhuan Karlus

503622




503622












  • The AJAX request is probably taking more than a second. Why aren't you using setTimeout?
    – SLaks
    Dec 27 '18 at 16:26










  • Actually, I'm using setTimeout. I didn't put it in the question. My interval executes every 5 seconds, and I've used a timeout of 2 seconds.
    – Rhuan Karlus
    Dec 27 '18 at 16:35












  • What are you trying to do? I mean, what is the goal? it doesn't really make much sense to use an interval to repeat an http call. I guess the goal is to execute an http call every X seconds, as long as one is not running already, but this code doesn't really serve this purpose.
    – briosheje
    Dec 27 '18 at 16:36










  • the message is shown twice because you clear the interval when you get the response not when you start the request. This means that during the latency since the first response, the interval is still starting ajax requests
    – quirimmo
    Dec 27 '18 at 16:37










  • Sorry guys, I've edited the code. Now you can help me?
    – Rhuan Karlus
    Dec 27 '18 at 16:39




















  • The AJAX request is probably taking more than a second. Why aren't you using setTimeout?
    – SLaks
    Dec 27 '18 at 16:26










  • Actually, I'm using setTimeout. I didn't put it in the question. My interval executes every 5 seconds, and I've used a timeout of 2 seconds.
    – Rhuan Karlus
    Dec 27 '18 at 16:35












  • What are you trying to do? I mean, what is the goal? it doesn't really make much sense to use an interval to repeat an http call. I guess the goal is to execute an http call every X seconds, as long as one is not running already, but this code doesn't really serve this purpose.
    – briosheje
    Dec 27 '18 at 16:36










  • the message is shown twice because you clear the interval when you get the response not when you start the request. This means that during the latency since the first response, the interval is still starting ajax requests
    – quirimmo
    Dec 27 '18 at 16:37










  • Sorry guys, I've edited the code. Now you can help me?
    – Rhuan Karlus
    Dec 27 '18 at 16:39


















The AJAX request is probably taking more than a second. Why aren't you using setTimeout?
– SLaks
Dec 27 '18 at 16:26




The AJAX request is probably taking more than a second. Why aren't you using setTimeout?
– SLaks
Dec 27 '18 at 16:26












Actually, I'm using setTimeout. I didn't put it in the question. My interval executes every 5 seconds, and I've used a timeout of 2 seconds.
– Rhuan Karlus
Dec 27 '18 at 16:35






Actually, I'm using setTimeout. I didn't put it in the question. My interval executes every 5 seconds, and I've used a timeout of 2 seconds.
– Rhuan Karlus
Dec 27 '18 at 16:35














What are you trying to do? I mean, what is the goal? it doesn't really make much sense to use an interval to repeat an http call. I guess the goal is to execute an http call every X seconds, as long as one is not running already, but this code doesn't really serve this purpose.
– briosheje
Dec 27 '18 at 16:36




What are you trying to do? I mean, what is the goal? it doesn't really make much sense to use an interval to repeat an http call. I guess the goal is to execute an http call every X seconds, as long as one is not running already, but this code doesn't really serve this purpose.
– briosheje
Dec 27 '18 at 16:36












the message is shown twice because you clear the interval when you get the response not when you start the request. This means that during the latency since the first response, the interval is still starting ajax requests
– quirimmo
Dec 27 '18 at 16:37




the message is shown twice because you clear the interval when you get the response not when you start the request. This means that during the latency since the first response, the interval is still starting ajax requests
– quirimmo
Dec 27 '18 at 16:37












Sorry guys, I've edited the code. Now you can help me?
– Rhuan Karlus
Dec 27 '18 at 16:39






Sorry guys, I've edited the code. Now you can help me?
– Rhuan Karlus
Dec 27 '18 at 16:39














1 Answer
1






active

oldest

votes


















0














The point is not about being the clearInterval sync or async.



In your code you start the interval the first time, and then after every second you execute your myCallback.



Clearing the interval when you get back a response, means that during the time that the ajax request is performing, the interval is still running and will start new ajax requests.






share|improve this answer





















  • Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
    – Rhuan Karlus
    Dec 27 '18 at 17:00










  • as I said above, it depends on how you manage the clearInterval
    – quirimmo
    Dec 27 '18 at 17:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948002%2fis-javascript-clearinterval-asynchronous%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The point is not about being the clearInterval sync or async.



In your code you start the interval the first time, and then after every second you execute your myCallback.



Clearing the interval when you get back a response, means that during the time that the ajax request is performing, the interval is still running and will start new ajax requests.






share|improve this answer





















  • Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
    – Rhuan Karlus
    Dec 27 '18 at 17:00










  • as I said above, it depends on how you manage the clearInterval
    – quirimmo
    Dec 27 '18 at 17:02
















0














The point is not about being the clearInterval sync or async.



In your code you start the interval the first time, and then after every second you execute your myCallback.



Clearing the interval when you get back a response, means that during the time that the ajax request is performing, the interval is still running and will start new ajax requests.






share|improve this answer





















  • Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
    – Rhuan Karlus
    Dec 27 '18 at 17:00










  • as I said above, it depends on how you manage the clearInterval
    – quirimmo
    Dec 27 '18 at 17:02














0












0








0






The point is not about being the clearInterval sync or async.



In your code you start the interval the first time, and then after every second you execute your myCallback.



Clearing the interval when you get back a response, means that during the time that the ajax request is performing, the interval is still running and will start new ajax requests.






share|improve this answer












The point is not about being the clearInterval sync or async.



In your code you start the interval the first time, and then after every second you execute your myCallback.



Clearing the interval when you get back a response, means that during the time that the ajax request is performing, the interval is still running and will start new ajax requests.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 27 '18 at 16:44









quirimmo

5,49911129




5,49911129












  • Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
    – Rhuan Karlus
    Dec 27 '18 at 17:00










  • as I said above, it depends on how you manage the clearInterval
    – quirimmo
    Dec 27 '18 at 17:02


















  • Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
    – Rhuan Karlus
    Dec 27 '18 at 17:00










  • as I said above, it depends on how you manage the clearInterval
    – quirimmo
    Dec 27 '18 at 17:02
















Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
– Rhuan Karlus
Dec 27 '18 at 17:00




Your answer is concise, but the alert is shown multiple times. I've used "twice" but it is shown as many time as my browser keeps open.
– Rhuan Karlus
Dec 27 '18 at 17:00












as I said above, it depends on how you manage the clearInterval
– quirimmo
Dec 27 '18 at 17:02




as I said above, it depends on how you manage the clearInterval
– quirimmo
Dec 27 '18 at 17:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948002%2fis-javascript-clearinterval-asynchronous%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'