9 Replies Latest reply on Jan 22, 2015 12:39 PM by NEW EXPORT ACCOUNT

    Make a JSON request refresh data every minute!

    NEW EXPORT ACCOUNT

      Hi,

       

      How to make an API request update automatically on a page at a specified duration? (every minute or so)

       

      Would basically like to be able to PUSH dynamic data via JSON to PhoneGap apps I build from Edge Animate projects.

       

      So, for instance, the JSON could contain info that generates a new symbol or starts a symbol playing, etc. Or updates an animation with wind speed from a weather API.

       

      Thanks in advance! Would be so cool! 

        • 1. Re: Make a JSON request refresh data every minute!
          Phildsb Level 1

          Try setInterval, e.g

          setInterval(function() { yourgetJSONfunction; }, 60000);

          Time interval in milliseconds.

          • 2. Re: Make a JSON request refresh data every minute!
            joel_pau Level 5

            Well , you have javascript or tigger options.

            • Javascript: setTimeout() or setInterval(),
            • Trigger can be added using code or timeline.

             

            Here is a javascript case:

            var request = function(){

            ...

            $.getJSON(...).done(...);

            ...

            setTimeout(request, 60000);

            };

            request();

             

            And you have to check the cache issue (browser cache).

            1 person found this helpful
            • 3. Re: Make a JSON request refresh data every minute!
              NEW EXPORT ACCOUNT Level 1

              Thanks for both of the posts! So...if I put "setInterval(function() { yourgetJSONfunction; }, 60000)" in composition.ready on the stage, then it will call at intervals continuously? And if I add that to an onclick function it will wait until the first click and then refresh at those intervals until the symbol is deleted?

               

              Basically like: create animated wind speed symbol, then have it update every so often...and have it deleted and replaced by a temp symbol if the temp is clicked. That's a basic example.

               

              Push notifications are also possible this way, right? Hoping to use it in PhoneGap to make quick prototypes.

               

              Thanks again and thanks in advance for clarifying! This is starting to get really cool.

              • 4. Re: Re: Make a JSON request refresh data every minute!
                Phildsb Level 1

                To start and stop it, make it a variable:

                var myInterval=setInterval(function(){ doSomething; },60000);

                To stop it, and remove the element with a click:

                sym.$('element').on('click',function(){ clearInterval(myInterval); sym.$('element').remove(); });

                For what its worth, you can do pretty much the same with setTimeout and clearTimeout. You'll have to decide which suits your requirements best.

                I've never tried push notifications, but I'm kind of interested in how they work now!

                • 6. Re: Re: Make a JSON request refresh data every minute!
                  NEW EXPORT ACCOUNT Level 1

                  Thanks! Wonderful to know. This goes into composition.ready?

                  • 7. Re: Re: Make a JSON request refresh data every minute!
                    NEW EXPORT ACCOUNT Level 1

                    This, I mean, initially...to set an interval that refreshes at intervals after page loads:

                    setInterval(function() { yourgetJSONfunction; }, 100);

                    Time interval in milliseconds.

                     

                    And then if I wanted to initiate that on a click, I'd add it to a click event? Would it run at an interval of 100 continuously, when launched by click?

                     

                    I guess the basic question is: Will this code somehow update at the intervals, regardless of where the code is placed? Or is composition.ready the only place where code like this runs properly?

                     

                    I am used to interactive products like Unity where there is a way to call functions on every frame, and the frames run constantly, regardless of any activity happening. I suppose if I put the function on a trigger that loops, and always have the timeline active, then it would update for sure.

                    • 8. Re: Make a JSON request refresh data every minute!
                      Phildsb Level 1

                      If you put it into composition.ready, it will then repeat until you stop it. I won't say you can't put it anywhere else because that depends on what you need, but its easier to understand the scope of your functions if they're attached to the Stage, particularly those that you want to call on from various elements within the composition.

                       

                      If you add it to a click event, be sure to clearInterval() otherwise you'll have multiple functions queuing up, e.g.

                      sym.$('element').on('click',function(){

                      clearInterval(myInterval);

                      myInterval=setInterval(function(){ doSomething; },60000);

                      });


                      Don't put setInterval() on a looping timeline though, you'd either get multiple events queuing up, or you'll end up clearing the interval (and cancelling the event) before its actually happened. But, if you want to use a timeline loop, you wouldn't need setInterval() or setTimeout() anyway, you could just fire the event each time the timeline reaches the trigger.

                      1 person found this helpful
                      • 9. Re: Make a JSON request refresh data every minute!
                        NEW EXPORT ACCOUNT Level 1

                        Thanks so much Phildsb!