0

Interesting problem with my pset8. Several odd behaviors are showing up. The first time you click on an icon for news stories, nothing happens. On the second click, whether on the same or a different icon, the news window will open, displaying news stories or "slow news day". Also, clicking on diffent icons may produce the same news list or a different list. Finally, sometimes clicking on a specific icon will produce "slow news day" but clicking again will produce a list of articles.

I have no idea why I'm getting this odd behavior. The code to retrieve the news stories follows. If more is needed, let me know.

// add listener for click and call articles.php
google.maps.event.addListener(marker, "click", function()  
{
     $.getJSON("articles.php?geo="+place.postal_code)
        .done(function(data, textStatus, jqXHR) {

        // call typeahead's callback with search results (i.e., places)
        articles = "<ul>";
        var len = data.length;

        for (var i = 0; i < len; i++) {
            articles += "<div><li> <a href=\"" + data[i].link + "\" target=\"_blank\">" 
                    + data[i].title + "</li></div>";
        }
        articles += "</ul>";

        if(len === 0){
            articles = "Slow News Day!";
        }
    })

    showInfo(marker, articles);        
} );
1

I haven't solved this year's yet, but I guess this may be because getJSON is asynchronous by default — it returns immediately.

you're calling showInfo right after getJSON, but sometimes showInfo is interpreted before the data is returned from article.php (i.e., before done is called). this is the case where I think nothing happens.

as for the weird behavior of not listing news then listing news when clicking on an icon twice, I think this is because article may a (global?) variable, from what I can see. in the case where showInfo is interpreted first, article is not updated yet, so current value of article gets passed to showInfo.

the solution should be obvious and that is to ensure that showInfo is called at the right time.

7
  • I understand your logic about asynchronicity, but I don't think that is the case. The two click requirement is absolutely consistent. It doesn't matter if I do it immediately or wait any length of time before or between clicks. If it were an issue of asynchronicity, I should see cases where sometimes two clicks are not required. There have been 0 cases of that. Also, on the second click, the news article list always returns immediately. I also noticed that the 'processing' icon never appears. Any other ideas?
    – Cliff B
    Nov 6 '15 at 18:41
  • @CliffB since we apparently agree that this is a problem, does that mean that you already fixed that and you still have the same problem?
    – kzidane
    Nov 6 '15 at 18:46
  • No, it means that I'm trying to interpret your latest edit and figure out how to apply it. I haven't sorted out exactly what change to make. To be honest, this is all new ground for me.
    – Cliff B
    Nov 6 '15 at 18:52
  • @CliffB what if you call showInfo at the end of done instead of after it?
    – kzidane
    Nov 6 '15 at 18:55
  • Your last suggestion fixed the problem. I moved showInfo() inside the code block and it cured it. thanks!
    – Cliff B
    Nov 6 '15 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .