How to call function recursively in Ajax with settimeout?

V

veg_all

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()>
 
R

RobG

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*. :)

[...]
 
T

Thomas 'PointedEars' Lahn

RobG said:
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
 
V

veg_all

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()>
 
R

RobG

Thomas said:
RobG wrote:




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.

[...]
 
T

Thomas 'PointedEars' Lahn

RobG said:
Thomas said:
RobG said:
(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top