0

I am trying to get json data from an external json file, I want to get data every second and the use it in other functions but when I console.log myData outside of the function it returns an empty object while when I console.log(myData) from outside it returns the data. How can I take this out of the function

    var myData = {};

    function getData(){
      var deferred = $q.defer();
      var path = 'http://orbit5.ds.cs.umu.se:8888/vrio/debug/blk'
      $http.get(path)
      .success(function(data){
        if(data) {
          myData = data
          deferred.resolve(data);
          console.log(myData)
        } else {
          console.log('cant process')
        }
      })
       return deferred.promise;

    }



   $timeout(getData, 1000)

    console.log(myData)

My question is regarding angular code I do not understand the answer I am directed to if someone can point me to a simpler explanation that would be nice

Imo
  • 1,455
  • 4
  • 28
  • 53
  • @musa Is there a simpler example I dont understand the one you have added – Imo Feb 10 '16 at 16:05
  • The duplicate question has 6 answers which have attracted at least 50 upvotes. If you don't understand something about one of them, then you could try asking a *more specific* question about the bit you don't understand (and reference that answer in your question). – Quentin Feb 10 '16 at 16:12

0 Answers0