Page 1 of 1

Help with Javascript

Posted: Mon 14 Mar 2011 10:39 am
by tjaliwalpa
I use js to update my current data webpage, some data on for today, on my iphone pages and also a banner on my website from the realtime.txt.

Is there some way to eliminate the garbage that occurs when the data is not available to the js. It i s easy to define the initial message before the data is read, but on the second and subsequent rereads, if the data is not read I get all sorts of messages including "undefined", "found", "undefinedthis".

Any ideas how to either get it to display previous data, waiting, unavailable or revert to previous data :?:

Re: Help with Javascript

Posted: Mon 14 Mar 2011 10:52 am
by mcrossley
Well, there are lots of techniques, depends on the situation.

A general catch for 'undefined' would be:

Code: Select all

var a = <something>;
if (a==undefined){
  a = "Missing data";
}

Re: Help with Javascript

Posted: Mon 14 Mar 2011 11:20 am
by beteljuice
JavaScript banner ?

Anyways - this is symptomatic of realtime.txt being uploaded whilst your other scripts are calling it.

If your server supports it try the 'rename' option in ftp. (if it isn't supported it won't update).

It sounds like you have three (or more) scripts that you would need to modify (double check what is already there) to be able to ignore an incomplete / corrupt file.

Re: Help with Javascript

Posted: Mon 14 Mar 2011 11:35 am
by mcrossley
If you are processing realtime.txt, presumably you are splitting into an array? If so check the array length before going any futher, if it is different than expected, retry the fetch from the website.

Code: Select all

 pseudo code
var array = objXML.responseText.split(" ");
if (array.length != expected_number_of_data_items>){
  call_function_to_restart_fetch();
}else{
  process_realtime_data();
}