Anand570611

The following code fetches data from a XML feed and just displays it on the gadget. Everything works fine except for one thing - the looping never takes off, the data that is fetched for the first time stays forever and there is no update.

Can someone help me understand why would this code not go and re-fetch the data from XML soruce every 25 seconds

Code Snippet

registerNamespace("Anand.Hariharan.V");

Anand.Hariharan.V.PopulationClock = function(p_elSource, p_args, p_namespace) {

Anand.Hariharan.V.PopulationClock.initializeBase(this, arguments);

var m_this = this;

var m_el = p_elSource;

var m_module = p_args.module;

var m_onDashboard = p_args.onDashboard;

var m_feedUrl = "http://www.------.com/rss/feed.xml f="

var m_numItems = p_args.numItems p_args.numItems : 5;

var m_feed = p_args.feed;

var m_moduleEl;

var m_isLoaded;

var m_headlinesEl;

this.initialize = function(p_objScope)

{

Anand.Hariharan.V.PopulationClock.getBaseMethod(this, "initialize", "Web.Bindings.Base").call(this, p_objScope);

GetFeed();

};

Anand.Hariharan.V.PopulationClock.registerBaseMethod(this, "initialize");

this.dispose = function(p_blnUnload) {

m_this = null;

m_el = null;

m_module = null;

m_onDashboard = null;

m_feedUrl = null;

m_moduleEl = null;

m_isLoaded = null;

m_headlinesEl = null;

Anand.Hariharan.V.PopulationClock.getBaseMethod(this, "dispose", "Web.Bindings.Base").call(this, p_blnUnload);

};

Anand.Hariharan.V.PopulationClock.registerBaseMethod(this, "dispose");

function callbackfn()

{

m_feedUrl = url+Math.random().toString();

setTimeout(GetFeed,25000);

}

function GetFeed()

{

var XML_Pointer = Web.Network.createRequest(

Web.Network.Type.XML,

m_feedUrl,

{proxy:"rss", numItems:m_numItems},

OnFeedReceived);

XML_Pointer.execute();

callbackfn();

}

function OnFeedReceived(response)

{

m_feed = Start.Parser.ParseRssResponse(response);

if (m_feed)

RenderFeed();

else if (!m_isLoaded)

m_el.innerHTML = "Error";

}

function RenderFeed()

{

if (m_feed && m_feed.channels && m_feed.channels.length > 0)

{

RenderChannel(m_feed.channels[0]);

}

}

function RenderChannel(channel)

{

if (!m_isLoaded)

{

if (m_el.innerText == "Loading...")

m_el.innerText = "";

m_isLoaded = true;

}

else

{

ClearFeed();

}

if (channel.items.length > 0)

{

RenderHeadlines(channel);

}

}

function RenderHeadlines(channel)

{

if (!m_headlinesEl)

{

m_headlinesEl = document.createElement("ul");

m_headlinesEl.className = "rssList";

m_el.appendChild(m_headlinesEl);

}

for (var i = 0; i < m_numItems; i++)

{

var li = document.createElement("li");

li.className = "rssItem";

m_headlinesEl.appendChild(li);

var aEl = document.createElement("a");

li.appendChild(aEl);

aEl.href = channel.items[i].link;

aEl.target = "top";

aEl.innerHTML = channel.items[i].title;

}

}

};

Anand.Hariharan.V.PopulationClock.registerClass("Anand.Hariharan.V.PopulationClock", "Web.Bindings.Base");




Re: Web Gadget Development Update parsed RSS data every 25 seconds

nguyentanbao

Hi,

There're couple of things that came to my mind (not sure it solves your problem or not):

1. Try status checking in your OnFeedReceived:

if (response.readyState == 4)
{
if (response.status == 200)
{

// YOUR CODE HERE

// callbackfn() here if you want to set timer for next fetch only when the current fetch succeed.

}

// callbackfn() here if you want to set timer for next fetch even the current fetch fail.

}

2. Have you tried checking the timeout works (debugging)

3. Just a suggestion (depends on your requirement): put callbackfn() as in 1. It means, after the fetching process finishes, we start the timer for the next fetch. Your current code means that, after send request, start the timer. In case, just in case, the processing time is bigger than 25s, the next fetch will overlap the current fetch.

4. You should save the timer so when dispose you can clear the timer. I experienced if it's not cleared, it might run one last time after the gadget unloaded. And this will cause error dialog appears.

Hope that helps.






Re: Web Gadget Development Update parsed RSS data every 25 seconds

Anand

Thanks for the reply.

And yes, Timeout works perfectly gine, i checked via debug. It just does not update the dispalyed data values






Re: Web Gadget Development Update parsed RSS data every 25 seconds

nguyentanbao

Look at the function:

function callbackfn()

{

m_feedUrl = url+Math.random().toString();

setTimeout(GetFeed,25000);

}

url is undefined. That's why from the second time it does not work.

The timeout doesn't work for me. setTimeout is never called because there's exception on m_feedUrl = url+Math.random().toString();






Re: Web Gadget Development Update parsed RSS data every 25 seconds

Anand

You have a point here, but if it did cause any exception then it should've come up sometime.

Anyway, I figured out another way of doing the same looping and it works fine as of now.

Thanks for your time, really!






Re: Web Gadget Development Update parsed RSS data every 25 seconds

ToddOs

Anand wrote:

You have a point here, but if it did cause any exception then it should've come up sometime.

Not necessarily true. The web gadget framework has a nasty way of eating exceptions every now and then. In this case, since you're going through the network stack (Web.Network.createRequest()), it's likely that ate exceptions that would've otherwise been thrown. In those cases, the only way to know there's a problem is to observe funky behavior, and then debug to find where the exception gets hidden by the framework.