Take the 2-minute tour ×

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?


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) {
                } else {
    function finalize() {
        $.getJSON("votingend", function(voting) {
            if (voting != null) {

Think this is most relevant, no?

share|improve this question
what is your code? (what does it look like? can you make a jsfiddle.net example?) –  Neal Jun 7 '11 at 16:54
can't make a jsfiddle example - the server is only locally accessible. it's not a productive environment. I updated my question however. thanks in advance –  chzbrgla Jun 7 '11 at 16:57
did you try outputting voting to see if it contained anything? –  Neal Jun 7 '11 at 16:59
Not explicitly.. but I called the finalize() function on it's own and it worked. –  chzbrgla Jun 7 '11 at 17:04
im talking about voting in the tick() fn –  Neal Jun 7 '11 at 17:05
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
it does actually stop the interval :) so I don't think there is the problem. thanks anyways! appreciate it. –  chzbrgla Jun 7 '11 at 17:04
argh.. dumb me. It's not stopping the interval.. the script is stopped because of the exception.. yup - most likely the problem here :) –  chzbrgla Jun 7 '11 at 17:12
Hehe :) I was about to post a jsfiddle to prove you wrong ;) Glad to see you figured it out. –  villecoder Jun 7 '11 at 17:14
add comment

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);
share|improve this answer
add comment

Your Answer


