By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

How to call function recursively in Ajax with settimeout?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.