473,394 Members | 1,658 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,394 software developers and data experts.

How to call function recursively in Ajax with settimeout?

I am trying to write an ajax script that keeps hitting my server every
10 seconds until it receives a different response . I am using
settimeout, but I think my script does not seem to work: Here is the
code I am using:

<SCRIPT language="javascript" type="text/javascript">

var url ="http://www.../check_last_updated";

var first_updated;

<!-------------------------------------------------------------------->

function handleHttpResponse() {

if (http.readyState == 4) {

last_updated = http.responseText;

if ( ! first_updated ) { first_updated = last_updated; }

if ( last_updated - first_updated > 0 )
{ document.write('Page has been updated'); }

else { setTimeout( "get_last_updated()", 10000 ); }
}
} // end function

<!-------------------------------------------------------------------->

function get_last_updated() {

http.open( 'get', url );
http.onreadystatechange = handleHttpResponse;
http.send(null);

} // end function

<!-------------------------------------------------------------------->

var http = getHTTPObject(); // We create the HTTP Object

<!-------------------------------------------------------------------->

</script> </head>

<body onload=get_last_updated()>

Jan 9 '06 #1
6 9269
ve*****@yahoo.com wrote:
I am trying to write an ajax script that keeps hitting my server every
10 seconds until it receives a different response . I am using
settimeout, but I think my script does not seem to work: Here is the
code I am using:

<SCRIPT language="javascript" type="text/javascript">
The language attribute is deprecated, remove it. Keep type.

var url ="http://www.../check_last_updated";

var first_updated;

<!-------------------------------------------------------------------->


Search the archives for the phrase 'potentially harmful'.

Now you know why it is recommended that HTML comment delimiters never be
used inside script elements - they serve no useful purpose and are
*potentially harmful*. :-)

[...]

--
Rob
Jan 10 '06 #2
RobG wrote:
ve*****@yahoo.com wrote:
var url ="http://www.../check_last_updated";

var first_updated;

<!-------------------------------------------------------------------->


Search the archives for the phrase 'potentially harmful'.

Now you know why it is recommended that HTML comment delimiters never be
used inside script elements - they serve no useful purpose and are
*potentially harmful*. :-)


Those are 69 dashes (wc -c rules :)) which is not a multiple of 4, hence the
comment, if parsed as such despite standards, is not closed (remember that
`--' opens a comment and `--' closes it within an SGML markup declaration
opened with `<!' and closed with `>'). It will probably work with one dash
less as 68 is a multiple of 4, but your assessment and recommendation would
still be mine even then.

The OP should use single-line comments instead:

// --------------------------------------------------------------------
PointedEars
Jan 10 '06 #3
Thanks, I made the changes to the script:
<SCRIPT type="text/javascript">

var url ="http://www.../check_last_updated";

var first_updated;

////////////////////////////////////////////////////////////////////

function handleHttpResponse() {

if (http.readyState == 4) {

last_updated = http.responseText;

if ( ! first_updated ) { first_updated = last_updated; }

if ( last_updated - first_updated > 0 )
{ document.write('Page has been updated'); }

else { setTimeout( "get_last_updated()", 10000 ); }
}

} // end function

////////////////////////////////////////////////////////////////////

function get_last_updated() {

http.open( 'get', url );
http.onreadystatechange = handleHttpResponse;
http.send(null);

} // end function

var http = getHTTPObject(); // We create the HTTP Object

////////////////////////////////////////////////////////////////////
</script> </head>

<body onload=get_last_updated()>

Jan 10 '06 #4
Thomas 'PointedEars' Lahn wrote:
RobG wrote:

ve*****@yahoo.com wrote:
var url ="http://www.../check_last_updated";

var first_updated;

<!-------------------------------------------------------------------->


Search the archives for the phrase 'potentially harmful'.

Now you know why it is recommended that HTML comment delimiters never be
used inside script elements - they serve no useful purpose and are
*potentially harmful*. :-)

Those are 69 dashes (wc -c rules :)) which is not a multiple of 4, hence the
comment, if parsed as such despite standards, is not closed (remember that
`--' opens a comment and `--' closes it within an SGML markup declaration
opened with `<!' and closed with `>'). It will probably work with one dash
less as 68 is a multiple of 4, but your assessment and recommendation would
still be mine even then.


It's actually 68 dashes :-p so an HTML validator will breeze by.

I find referring to SGML confuses people, hence I use the term "HTML
comment delimiters". It's not as technically accurate as "SGML markup
declaration" but it seems to be more widely understood.

[...]

--
Rob
Jan 10 '06 #5
ve*****@yahoo.com wrote:
Thanks, I made the changes to the script:


Maybe this will save you some grief - check out the request library:

<URL: http://www.ajaxtoolbox.com/request/ >

[...]

--
Rob
Jan 10 '06 #6
RobG wrote:
Thomas 'PointedEars' Lahn wrote:
RobG wrote:
ve*****@yahoo.com wrote:
<!-------------------------------------------------------------------->
Search the archives for the phrase 'potentially harmful'.

Now you know why it is recommended that HTML comment delimiters never be
used inside script elements - they serve no useful purpose and are
*potentially harmful*. :-)

Those are 69 dashes (wc -c rules :)) which is not a multiple of 4, hence
the comment, if parsed as such despite standards, is not closed (remember
that `--' opens a comment and `--' closes it within an SGML markup
declaration opened with `<!' and closed with `>'). [...]


It's actually 68 dashes :-p so an HTML validator will breeze by.


I recounted it manually and you are right. This is the reason why wc -c
seemed to count "wrong":

$ wc -c <<<xa
3

$ od -c <<<--------------------------------------------------------------------
0000000 - - - - - - - - - - - - - - - -
*
0000100 - - - - \n \0
0000105

The shell script syntax used to count characters adds a newline character
to the input.

$ echo -n '--------------------------------------------------------------------' | wc -c
68
Regards,
PointedEars
Jan 10 '06 #7

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

Similar topics

3
by: David Shorthouse | last post by:
Hey folks, Not an off-topic posting.....since I was shot-down in an earlier post...this one's legit. How do I go about calling a server-side vbscript within a client-side javascript function?...
3
by: Dica | last post by:
this works: document.all.style.visibility = 'hidden'; this doesn't setTimeout("document.all.style.visibility = 'hidden'",1000); looks like sResponseDivID isn't recognized. what's the problem?...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
9
by: Jordan Pittman | last post by:
ok i want to wait an x amout of seconds before my ajax script writes the results to a div, but i want it to still display the loading bar, i do have a variable http_rste that holds the readyState...
2
by: Peter Michaux | last post by:
Hi, I am trying out the Ajax timeout example in Flanagan's book p492. It works fine in Opera but in Firefox the the call to request.abort() doesn't stop onreadystatechange being called. I found...
3
by: nghivo | last post by:
I attempted to synchronize async Ajax calls using the following JS blocks: ==================================================== function getXMLHTTPRequest() { try { req =...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
3
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.