473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Collisions & priorities: xmlHttpRequest and setInterval

From my newbie perspective I understand that Javascript is a single
threaded environment and so interrupt-driven events are going to be
somewhat challenging.

But, I have an issue which seems to be to be a collision between a
periodic drawing event (based on "setInterval") and a periodic http
request based on the "xmlHttpRequest.onreadystatechange" listener.

I assume my interval timeout, and the operations associated with that
occasionally make me miss my http reply.

Two questions
a) given that one of these (the receive) is more important to me than
the other (the setInterval) can I set priorities in some way?

b) is there some other good method for handling such interrupt
collisions? I'm hoping to avoid the whole process of associating
receives with the requests that spawned them, and doing re-transmission
requests etc, A time-out is prudent no doubt, but I'd like to give my
receives a fighting chance.

Thanks for any thoughts...

Ross.
Jul 10 '08 #1
4 2666
RgeeK <Ro**@no.thanks.spammerswrites:
From my newbie perspective I understand that Javascript is a single
threaded environment and so interrupt-driven events are going to be
somewhat challenging.
It's a good thing that javascript (at least in browsers) doesn't need
to handle interrupts, then. :-)
But, I have an issue which seems to be to be a collision between a
periodic drawing event (based on "setInterval") and a periodic http
request based on the "xmlHttpRequest.onreadystatechange" listener.

I assume my interval timeout, and the operations associated with that
occasionally make me miss my http reply.
What do you base that assumption on?
Two questions
a) given that one of these (the receive) is more important to me than
the other (the setInterval) can I set priorities in some way?
No. At least not in any portable way that I know of.
b) is there some other good method for handling such interrupt
collisions?
Neither of those are interrupts in any sense that I know of. It's just
bog-standard event handling. Meaning; one event is handled, then the
other event is handled. From a javascript point of view, events
*never* occur at the same time.

What will happen if you starve the CPU by running massive amounts of
long-running events is not really defined, though.
I'm hoping to avoid the whole process of associating receives with
the requests that spawned them, and doing re-transmission requests
etc, A time-out is prudent no doubt, but I'd like to give my
receives a fighting chance.
You may want to reduce the work you do in any single event
handler. And also maybe increase the interval for your setInterval
events.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 10 '08 #2
Thanks for your reply.

Joost Diepenmaat wrote:
>
What do you base that assumption on?
This is based on some behaviour of app that seems to be missing receive
events in response to my server requests. The missed responses increase
in frequency as I make the interval smaller in the other task.

I'm new enough to JS that I don't pretend to know how the event handling
works, but the idea that one might be pre-empting the other made me
think it was behaving more like prioritized interrupts.
Neither of those are interrupts in any sense that I know of. It's just
bog-standard event handling. Meaning; one event is handled, then the
other event is handled. From a javascript point of view, events
*never* occur at the same time.
That's reassuring, so I will certainly dig into this further to see if I
can get around it. I guess though that the priority issue is still a
central one as I think about this. If my interval driven code runs and
takes CPU attention away from servicing the arriving httpresponse, I
suppose I could conceivably miss the data when I finally get the chance
to go service that http data-ready event? Perhaps the buffers are
flushed in preparation for the next arrival before I can get at the data?

What will happen if you starve the CPU by running massive amounts of
long-running events is not really defined, though.
My events aren't very CPU intensive - so I don't expect to be really
hammering it.
>
You may want to reduce the work you do in any single event
handler. And also maybe increase the interval for your setInterval
events.
I know the interval increase works - but given that I'm driving some
graphics, lengthening the interval degrades the visuals. However,
that's what made me concerned about missed receive events: with
lengthened intervals I can account for all the receive events. With
shorter intervals I miss several, up to the point where intervals in the
10ms range ( I assume those are mS units) mean I don't manage to process
any receives at all.

Thanks for the comments!

Ross.
Jul 11 '08 #3
RgeeK <Ro**@no.thanks.spammerswrites:
Thanks for your reply.

Joost Diepenmaat wrote:
>>
What do you base that assumption on?
This is based on some behaviour of app that seems to be missing
receive events in response to my server requests. The missed
responses increase in frequency as I make the interval smaller in the
other task.
What kind of events are we talking about here? Servers don't send out
requests. Browsers do. At least if your using XmlHTTPRequest You
should *at least* get a readystatechange event when the document is
fully loaded (readyState == 4). I'm not sure that any of the other
state changes are reliable, though (especially if your browser is
caching the responses).
>What will happen if you starve the CPU by running massive amounts of
long-running events is not really defined, though.
My events aren't very CPU intensive - so I don't expect to be really
hammering it.
Are you sure? Animations can take up a lot of CPU time, even if the JS
code does hardly anything.

One thing you could try is instead of using setInterval, re-register
your event using setTimeout at the end. that should ensure you've got
some time left to handle any other events in between even if your
timed events take longer than the interval:

function timerHandler() {
// do your animation here

// call again in 50 ms
setTimeout(timerHandler,50);
}
timerHandler();


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 11 '08 #4
On Jul 11, 3:59*pm, RgeeK <R...@no.thanks.spammerswrote:
This is based on some behaviour of app that seems to be missing receive
events in response to my server requests. *The missed responses increase
in frequency as I make the interval smaller in the other task.

(...)

If my interval driven code runs and
takes CPU attention away from servicing the arriving httpresponse, I
suppose I could conceivably miss the data when I finally get the chance
to go service that http data-ready event? *Perhaps the buffers are
flushed in preparation for the next arrival before I can get at the data?

(..)

I know the interval increase works - but given that I'm driving some
graphics, lengthening the interval degrades the visuals. *However,
that's what made me concerned about missed receive events: with
lengthened intervals I can account for all the receive events. With
shorter intervals I miss several, up to the point where intervals in the
10ms range ( I assume those are mS units) mean I don't manage to process
any receives at all.
Even under heavy load (a setInterval(f, 0), being f() a cpu-intensice
time-waster procedure), all the browsers I test manage to dispatch
properly to all the (25 simultaneous) posted XHR's objects' callbacks,
and never miss a single one. I'd bet the problem lies in your code,
not in the browsers'.

Test it yourself : http://tinyurl.com/5sx3jr

--Jorge.
Jul 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Noh Way | last post by:
I have a stack of DOM elements and I have stored their IDs in an array. I want to press a button - this code is from that button's event's function- and have a slideshow as each element is brought...
2
by: Marco Laponder | last post by:
Hi All, I am using XmlHttpRequest object within Firefox to get a xml document from the servlet. The reponseText is set but the responseXml is null. My Code is: req = new XMLHttpRequest();...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
5
by: Matt Kruse | last post by:
I'd like to test for Opera8.00's missing setRequestHeader method before actually instantiating the object. For example, this works in firefox: if (XMLHttpRequest.prototype.getRequestHeader) { ......
12
by: PMA | last post by:
Hi all, I am porting a web application from IE 6.0 to FireFox 1.5. I have solved almost all compatibility issues (quite a lot but not too bad) except two of them : 1) Clipboard access thru'...
3
by: Ivan P | last post by:
Hello! I have a index.php that on one click calls via AJAX a file.php. index.php looks like this: <html > <head> <script language="javascript" type="text/javascript" >
1
by: Chris117 | last post by:
I am trying to update two different parts of a page at different intervals. I have a setInterval() function that calls the function to update the appropriate part of the page. When the script is...
4
by: libsfan01 | last post by:
Hi all How can i modify my code so that js will continously check the data on a page (data.php) and bring it through. at the moment it only does it once when the function is called, how can i...
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.