开发者

Nested JSON GET functions

I am looking for a way to have a .getJSON, call another function which in return conducts another .getJSON call.

I'm iteratively calling a JSON method on my Controller and want to finish this iterative cycle if a certain condition is met. If this condition is met (can only be checked from within the JSON method), I want to call another JSON method on the Controller (persisting some data etc.).

However, the second JSON call is never made. When I'm callin the second function manually outside of the first function, the call is made correctly.

Any hints on that or is my design utterly flawed anyways?

EDIT

Chyeaah.. forgot the code.. :D

 <script type="text/javascript">
    $(window).load(function() {
        var poller = setInterval(tick, 1000);
    });
    function tick() {
        $.getJSON("votingtick", function(voting) {
            if (voting != null) {
                if (voting.timeleft < 0) {
                    clearInterval(poller);
                    finalize();
                } else {
                    $("#timeout").text(voting.timeleft);
                    $("#voters").text(voting.votingCount);
                }
            }
        });
    }
    function finalize() {
        $.getJSON("votingend", function(voting) {
            if (voting != null) {
                $("#finished").fadeIn('fast');
            }
        });
    }
</script>
开发者_C百科

Think this is most relevant, no?


I suspect that you have a scope problem with your poller variable. It is probably only defined in your $(window).load function and nowhere else. So when the browser attempts to execute the clearInterval, an exception is thrown and the script is aborted. You can verify this by surrounding the code in your $.getJSON function with a try catch.

If you remove the var keyword in that function, poller will become a global variable that you can reference anywhere in the script.


It's probably because you get an error when you call clearInterval with an undefined value. You declare the variable poller locally in the load event handler, so it's undefined outside it.

Declare it outside the function:

var poller;
$(window).load(function() {
    poller = setInterval(tick, 1000);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜