Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Central

XMLHttpRequest works once, but won't refresh

Author Message
JohnGaver



Joined: 17 May 2005
Posts: 20
Location: Texas

Posted: Sun Jun 12, 2005 - 5:49 am    Post subject: XMLHttpRequest works once, but won't refresh Reply with quote

I am picking up a 3-digit number from a file on the internet, using XMLHttpRequest(). It works when I first open the widget, but subsequent calls to the function do not refresh the responseText.

It is first called in setup(), when the widget opens. In trying to make my widget refresh, I have tried to call it via setTimeout(...), window.onload and onshow(). However, when the file on the internet changes, the widget will not refresh, without completely exiting the widget and restarting it.

Here is the code for the function that uses the XMLHttpRequest. I also included the line where I call it, with window.onload. Any ideas???

Quote:
function wbtrCount() {
var crashes = new XMLHttpRequest();
crashes.open("GET", "http://www.actionamerica.org/houston/wbtrcount.txt",true);
crashes.onreadystatechange=function() {
if (crashes.readyState==4); {
var rams = crashes.responseText;
document.getElementById("crash").innerHTML = rams;
}
}
crashes.send(null);
setTimeout("wbtrCount()", 30000);
}

window.onload = wbtrCount();
View user's profile Send private message Send e-mail Visit poster's website Widgets
Chris



Joined: 27 Jan 2005
Posts: 344
Location: Durham, UK

Posted: Sun Jun 12, 2005 - 7:02 am    Post subject: Reply with quote

You're likely seeing the caching problem that has been discussed on these forums before.

You need to tell it not to cache by using:

crashes.setRequestHeader("Cache-Control", "no-cache");

after you open() the request.

Also, you realise that your onload handler won't actually be getting called onload? Since you've add the parenthesis, the JS parser will execute the function as it sees it, rather than onload. Instead do

window.onload = wbtrCount;

You're getting away with it because by the time the readystate of the request reaches 4, then the rest of the page will have loaded. You'd see the problem if you referenced some elements on the page immediately in your onload function, since you'd get a DOM error due to them not existing yet (assuming you include the JS at the top before other elements are loaded).
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
JohnGaver



Joined: 17 May 2005
Posts: 20
Location: Texas

Posted: Sun Jun 12, 2005 - 12:55 pm    Post subject: That worked - thanks Chris Reply with quote

D@#%, I knew that!

Thanks, Chris. I haven't fooled around in this area of JS in a long time. I have recently only been using it for basic web site functionality. I knew that it was something simple that I was overlooking.

Look for the Houston Wham-Bam-Tram Ram Counter widget to appear soon. It will keep up with the all too common crashes of the woeful little tram that we have here in Houston, as reported on the Action America Houston Pages. I'm just not completely satisfied with the appearance of the back side, yet.

It will update when the widget starts up, whenever the dashboard is opened, whenever the widget is flipped and if the dashboard is left open, it will also update every 10 minutes.

Thanks again.
View user's profile Send private message Send e-mail Visit poster's website Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group