Hi
How is it possible to write Ajax code using
var timerId = setTimeout("test_function();", 250);
and
xmlhttp.onreadystatechange=function()
but without the user doing anything.
Thanks in advance,
Lawrence
Hi
How is it possible to write Ajax code using
var timerId = setTimeout("test_function();", 250);
and
xmlhttp.onreadystatechange=function()
but without the user doing anything.
Thanks in advance,
Lawrence
Hmm... You mean you want an auto refresh? Maybe something as follow could give you an idea...
var timerId
function callAutoRefresh() {
timerId = setTimeout("testFunction()", 250)
}
function testFunction() {
// do the ajax call here
// at the end of the part inside onreadystatechange and status==200
// call the setTimeout("testFunction()", 250) again
}
Hi Taywin
I changed my code to look like this:-
<script language="javascript" type="text/javascript">
var timerId;
function callAutoRefresh()
{ timerId = setTimeout("introFunction();", 250);
}
function introFunction()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("NofP").innerHTML=xmlhttp.responseText;
timerId = setTimeout("introFunction()", 250);
}
}
xmlhttp.open("GET","test.asp",true);
xmlhttp.send(null);
}
It works, but I still have to press a key to trigger the xmlhttp.onreadystatechange=function(). I was hoping that test.asp would be called every quarter of a second without any key presses.
Many thanks in advance
Lawrence
Not sure what you mean by 'press a key'? You could call the introFunction() once when a page is loaded. Then the thing should keep going by itself. Or you could call the introFunction() at the end of your page as well to start it off. How do you use the function in your page right now?
Hi
The following code works because of the alert statement. If you press the OK Alert button, everything is as it should be. When the code is rum without the alert statement, the code does not seem the run every 1/4 second. The initial code call seems to be okay and the xmlhttp.status s always 200.
function introFunction()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.status);
document.getElementById("NofP").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","test.asp",true);
xmlhttp.send(null);
timerId = setTimeout("introFunction();", 250);
}
Not sure what you mean... You mean that the code is being call after you put up an alert message? Or after the alert message? Or right at the time the alert message is closed (after clicking the 'OK' button)? For the last one (close on clicking on 'OK' button), I am not sure that you could do that.
Hi
Sorry for the confusion.
If I insert the alert statement at Line 15, everything works correctly. But of course, Í don't want an alert statement in the code.
introFunction is being called as a normal javascript call at the top of the webpage.
<script language="javascript" type="text/javascript">
var timerId = setTimeout("introFunction();", 250);}
</script>
It is strange that I need the code at Line 21.
The line numbers refer to the code in the previous post.
Many thanks
Lawrence
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.