Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setInterval and clearInterval functions  RSS feed

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to set javascript code that runs onload of a page.
The script will call a server side jsp every 5 seconds that will check if a message for the user exists, if yes it will alert it every X seconds using setInterval and if not it will not alert and clear any Intervals already been set.
The peroblem I'm having is that I can't seem to clear previous alerts that have already been set.
The problem is around the clearInterval of getMessage() function, I tried setting alertMessage as "static" by naming it getMessage.alertMessage but it didn't help.
I'm thinking of perhaps putting all alertMessage instances in an array and then clear it on every call to getMessage(0 function?

Here is my code:
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future when you post code, use code tags.

You can not hide an alert message, the user would have to dimiss it. Alerts is like a pause button, they stop execution of the JavaScript code and there is nothing you can do to make it asynchronous. If you want full control, you need to build you own alert type of sysytem with divs,

Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not interested in dismissing an alert that has already happened, I'm interested in disabling the setInterval I set on every call in case there is no message to display.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using an interval for the alert in the first place? If you are trying to delay it, it should be setTimeout and not interval.

Polling the server every 5 seconds can be costly if you have a lot of people on the site. Your server may hate you.

Are you always expecting a status of 200? you should be checking for it. You should set open before onreadystatechange. You should check native XMLHttp support before activeX.

Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,

I appreciate the observations but it doesn't answer the specific problem I am having here.
If there is a message on the server the requirement is to keep alerting it every X seconds/minutes and not just once, hence the setInterval.
In reality I wouldn't be polling from the server every 5 seconds, that was just for testing purposes to see how it works.
I will handle all response codes coming back from XMLHttp in due course, it is irrelevant for this problem.
Setting open before onreadystatechange doesn't make much difference since onreadystatechange is only called upon server response, like a method to handle an event.

Perhaps I wasn't clear on the problem with this code: If there is a message on the server setInterval is popping out message to the user as expected however if I remove the message from the server, I see it gets into the clearInterval section but the user is keep getting the alerts as if setInterval hasn't been cleared.
My assumption is that every call to the server set up new instance of setInterval and am looking of a better way to script that scenario.

Roy

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy Cohen wrote:
Setting open before onreadystatechange doesn't make much difference since onreadystatechange is only called upon server response, like a method to handle an event.


Actually it does make a difference. I am not going to go into details.

I actually ran your code with no issues. I bet the problem is something totally different than what you think it is. You are cached.

Add the following after open:



Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

I'm still unable to stop setInterval even after setting this line after open.

Roy
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the interval you are testing with? What is the browser and version?

I ran your code with thealertMessage = setInterval('alert(\"test\")',1000); with no issues.

Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing with the same line of code, using IE7
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works fine for me. I copied and pasted what you did and it worked okay.

What is the exact problem you are facing? How do you know the alert is not being canceled? I am flying blind here, you need to explain how you know it is not working/clearing.

Make sure you are not cached with an old version of your file, clear it.





Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

I know it is not getting cleared because when I clear the message on the server the jsp returns "" and I expect clearInterval to work and remove future messages however the alerts are keep coming in every interval.

Roy
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alert the response length.

Eric
 
Kaustav Ganguly
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The setInterval method returns an interval ID , you need to use this interval ID toclear the interval , so the code is as follows

var intervalID = setInterval(.....);

to clear the interval you should use the interval ID as

clearInterval(intervalID)

try using the interval ID , the interval should be cleared.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustav Ganguly wrote:The setInterval method returns an interval ID , you need to use this interval ID toclear the interval , so the code is as follows

var intervalID = setInterval(.....);

to clear the interval you should use the interval ID as

clearInterval(intervalID)

try using the interval ID , the interval should be cleared.


Look at the code, that is what it is doing.

Eric
 
Kaustav Ganguly
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,

Could you please point out the line in the code where the interval ID returned by the setInterval method is stored and used afterwards to clear the interval ??
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustav Ganguly wrote:Eric,

Could you please point out the line in the code where the interval ID returned by the setInterval method is stored and used afterwards to clear the interval ??




You are looking at the wrong interval.

Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

How did you exactly test the code ?
When you say you are able to run this code with no problem does it mean that the interval is being cleared for you?
Have you actually created a jsp to call?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy Cohen wrote:
How did you exactly test the code ?

I created a website and tested it with a plain text file [source does not matter] I edited the file and it worked fine.

Roy Cohen wrote:
When you say you are able to run this code with no problem does it mean that the interval is being cleared for you?

Yes

Roy Cohen wrote:
Have you actually created a jsp to call?

No, the source does not matter. You really should use a servlet and not a JSP.

Eric
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote: You really should use a servlet and not a JSP.



Isn't a jsp actually a Servlet?

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy, I just tested the first code you submitted in your first post. The only thing I changed was the URL for the GET request. It works as you are describing it needs to work. I am simply calling an empty file. When I add text to the file, I get an alert with 'test' in it over and over until I remove all the text from the file, then I no longer receive the 'test' alert.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, if you use a library like jQuery, you can really cut down on the boilerplate ajax code...

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks for all your help guys.
I will revisit my code and also check for the jQuery alternative.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!