473,786 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setTimeOut and atomicity

Hi,

I am working on a simple chat program and want to use AJAX. In order to
get the new lines added to the chat I need to call a server-side script,
say every x seconds.

A second way to get those lines is to get them as a reply to the "add this
line" request when someone pressed the send button (or just enter).

So my idea was to:

get lines, and add them to the chat
setTimeOut for 5 seconds to get lines
if someone presses send, remove the setTimeOut, and
after receiving lines, setTimeOut for 5 seconds

The main question is: are the onClick handler and the setTimeOut atomic?
What I mean, should I see a browser as handling events atomic, and both
are events, hence it's not possible that the function registered with
setTimeOut is called in the "middle" of the onClick handler?

Thanks,

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Dec 9 '05 #1
5 2548
A(synchronous) JAX is there to do just that. You can send a response
out and sit there, waiting for the response. When the response is
received, it triggers an event handler. You can have it call a
function. No need for timeouts. It is the server's responsibility to
send the response.

I made a simple framework based on the 'sack of ajax' framework, but
better ;). It will parse a response XML into an array so you don't have
to worry about traversing the structure. Let me know if you would like
it.

-Eric

--I had thought about doing something like this. I had previously done
a simple chat using Java (desktop and/or applets) for a project and was
fairly pleased, though javascript certainly has an advantage of not
needing the JVM.

Dec 9 '05 #2
"bobzimuta" <ej******@gmail .com> wrote:
A(synchronous) JAX is there to do just that. You can send a response
out and sit there, waiting for the response. When the response is
received, it triggers an event handler. You can have it call a
function. No need for timeouts.
Yes, there is a need, since I want to have new lines added even when the
user doesn't type at all. I know how AJAX works, and that it's async. The
question is more: what happens when the visitor presses send, and the
timeout is reached. Are such events handled one by one, or can the
SetTimeout handler interrupt the OnClick one (or vice versa)?
It is the server's responsibility to
send the response.

I made a simple framework based on the 'sack of ajax' framework, but
better ;). It will parse a response XML into an array so you don't have
to worry about traversing the structure. Let me know if you would like
it.


I probably am going to have the serverside prepare the HTML, so the client
side only has to add the HTML. Probably the structure will be very simple,
e.g.

<response>
<chatlines> HTML (encoded) </chatlines>
<userlist>HTM L (encoded)</userlist>
<response>

regarding: AHAH is it not needed that the XML (XHTML) is valid? I seem to
recall that with AHAH one just uses the response "XML" directly, and not
the parsed result.

Moreover, what's the best way to encode the HTML? I was thinking of using
base64, but I recall that JavaScript has no encode/decode?
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Dec 10 '05 #3
On 2005-12-09, John Bokma <jo**@castleamb er.com> wrote:
Hi,

I am working on a simple chat program and want to use AJAX. In order to
get the new lines added to the chat I need to call a server-side script,
say every x seconds.
just call it and when it returns call it again have the server delay
its response if there is no new matterial, limit the delay to about 20
seconds (so stuff does't time out)
this way a correctly written server can give near immediate response to posted
lines instead of having to wait for the client to request, and also the idle
bandwidth is reduced.

this of course relies on you having access to a server that's capable of
this... this "getting" is the sort of thing that lends itself to a custom
written HTTP server that can track the posts and store a few minutes worth
in memory and queue up requests when there's no content to rend in response
.... - here there's no advantage to the separate application methodology
that's used for traditional CGI.
A second way to get those lines is to get them as a reply to the "add this
line" request when someone pressed the send button (or just enter).
yes (but that's can get annoying) with the above running asynchronously
use a separate ajax call to post..
So my idea was to:

get lines, and add them to the chat
setTimeOut for 5 seconds to get lines
if someone presses send, remove the setTimeOut, and
after receiving lines, setTimeOut for 5 seconds

The main question is: are the onClick handler and the setTimeOut atomic?
AFAIK while javascript is executing the all other events are suspended.
What I mean, should I see a browser as handling events atomic, and both
are events, hence it's not possible that the function registered with
setTimeOut is called in the "middle" of the onClick handler?


I thik JS is aingle-streaming, so no the timout handler won't iinterrupt
the click handler or vice versa. (but I think yoshoulduse readyStateChang e
on the request instread of a timout to trigger the downloads.

don't use setimeout on the client. and there's no need to use the posting
requests to retrieve content, keep them seperate - the code is easier.

sometimes a requests arrive out of order - possibly a good reason to no mix
downloading pathways

Bye.
Jasen
Dec 10 '05 #4
Jasen Betts <ja***@free.net .nospam.nz> wrote:
On 2005-12-09, John Bokma <jo**@castleamb er.com> wrote:
Hi,

I am working on a simple chat program and want to use AJAX. In order
to get the new lines added to the chat I need to call a server-side
script, say every x seconds.
just call it and when it returns call it again have the server delay
its response if there is no new matterial, limit the delay to about 20
seconds (so stuff does't time out)


20 seconds is a long, long time in a chat :-)
this way a correctly written server can give near immediate response
to posted lines instead of having to wait for the client to request,
and also the idle bandwidth is reduced.
Sounds like a good idea, will think about it.
A second way to get those lines is to get them as a reply to the "add
this line" request when someone pressed the send button (or just
enter).


yes (but that's can get annoying) with the above running
asynchronously use a separate ajax call to post..


But then still the question remains: how atomic is this? I don't want to
end up with a part of one line, then a new line, followed by the rest.
So my idea was to:

get lines, and add them to the chat
setTimeOut for 5 seconds to get lines
if someone presses send, remove the setTimeOut, and
after receiving lines, setTimeOut for 5 seconds

The main question is: are the onClick handler and the setTimeOut
atomic?


AFAIK while javascript is executing the all other events are
suspended.


So basically there is one even queue, and they are processed in order?
Or does suspend mean that execution of an event handler is interrupted
somewhere in the middle, and another gets started?
What I mean, should I see a browser as handling events atomic, and
both are events, hence it's not possible that the function registered
with setTimeOut is called in the "middle" of the onClick handler?


I thik JS is aingle-streaming, so no the timout handler won't
iinterrupt the click handler or vice versa. (but I think yoshoulduse
readyStateChang e on the request instread of a timout to trigger the
downloads.


But I want to know for sure ;-)
don't use setimeout on the client. and there's no need to use the
posting requests to retrieve content, keep them seperate - the code is
easier.


Ok, I will have a go at your idea, which is interesting, thanks.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Dec 10 '05 #5
John Bokma wrote:
The main question is: are the onClick handler and the setTimeOut atomic?
What I mean, should I see a browser as handling events atomic, and both
are events,
setTimeout() sets a timeout, that is not an event.
hence it's not possible that the function registered with
setTimeOut is called in the "middle" of the onClick handler?


If you ask whether JS/ECMAScript is single-threaded, the answer is "yes".
PointedEars
Dec 10 '05 #6

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

Similar topics

3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
4672
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script, it should simply present an alert message after 5 seconds. Thanks. <html> <head> <script language="javascript"> function MsgTimer() { var self = this;
6
2253
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id = document.getElementById(divId); setTimeout(id.style.display = 'none',timeout); } Thanks for any help.
12
5556
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
28
5311
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
4
5235
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
15
3796
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
7
2415
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to interfer with one another.
3
1261
by: kirk | last post by:
I have a class with a custom event that starts a timer when it's holding subscriptions and stops the timer otherwise. The timer repeatedly calls an API which takes a string. For each call the return value of the API is compared against the last obtained value from it and if different, I fire my custom event. The string that the API is using for its input arg is defined by a public property to this class. My question is, how can I...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.