1

Sup guys !

I'm currently have some problems about the synchronization with JavaScript. It's about a refresh. When it reach 10 second i refresh an array.

var curD = new Date();
if((curD.getTime() / 1000) - dMList[i].startTime > dMList[i].refreshTime)
{
    dMList[i].elem = new Array();
    RSSDropMenuConstructor(dMList[i]);
    dMList[i].startTime = curD.getTime() / 1000;
}

sendResponse({ getdropMenuFields: dMList[i] }); // Send the appropiate dropMenu

Function RSS

function RSSDropMenuConstructor(dMObject)
{
    jQuery.getFeed({
       url: dMObject.rssLink,
       success: function(feed) {

        for(var i = 0; i < feed.items.length && i < dMObject.maxItem; i++) {

            var item = feed.items[i];

            field = new Object();               
            field.name = 'text';
            field.value = item.title;
            dMObject.elem.push(field);

            field = new Object();
            field.name = 'weblink';
            field.value = item.link;
            dMObject.elem.push(field);

            field = new Object();
            field.name = 'icon';
            field.value = 'http://mediacdn.disqus.com/1305270873/images/embed/bullet-feed.png';
            dMObject.elem.push(field);
        }          

        dMList.push(dMObject);
   }
});

So the RSSDropMenuConstructor will construct the dMList[i].elem, but the problem is that then "SendReponse" don't wait the end of the function.

So what it happend, when it refresh i received an empty array because the array is not yet initialized... Somebody have an idea how i can synchronize this ?

4
  • can you post some more code ? is this given code in a function ? how you are calling the function ? Commented May 13, 2011 at 15:20
  • I don't really get why the sendResponse call is outside of the if statement. Do you call this periodically ? Commented May 13, 2011 at 15:24
  • Yeah, it's because, if i need to refresh or not, in both case i need to send a response Commented May 13, 2011 at 15:37
  • Is dmList global ? Seeing the calling code for the first bit would help :) Commented May 13, 2011 at 16:13

2 Answers 2

1

You need to wait for the feed to come back from the server before calling sendResponse(). Place the call to sendResponse() in the callback function (that is, function(feed){...}).

Because AJAX is asynchronous, Javascript will continue to execute your code, even while a request to the server is pending. The callback, on the other hand, does not execute until the server responds.

Sign up to request clarification or add additional context in comments.

Comments

0

Put the sendReponse() function inside the if statement. That way it will execute when the rest of the code inside that block is executing.

1 Comment

Without knowing what else you are calling, it's hard to help any further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.