javascript - What is the scope of the callback on my vanilla JSONP -


a little backstory, i'm using twitch (game streaming service) api pull list of followers channel on site. in order around cors rules i'm using jsonp. personally, prefer use vanilla javascript on libraries learned how excellent article on over https://cameronspear.com/blog/exactly-what-is-jsonp/ .

below simplified javascript (the actual code isn't extremely necessary, maybe it'll clarify question afterward):

//function pull jsonp  function pulljsonp(url) {      var jsonpinstance = document.createelement('script');     jsonpinstance.src = url;      jsonpinstance.onload = function () {         this.remove();     };      var head = document.getelementsbytagname('head')[0];     head.insertbefore(jsonpinstance, null); }    //end function pull jsonp   function storecurrentfollows(offset) //function wrap entire operation {       function pullstartsessionfollows(jsondata) //callback jsonp down below     {            function pushnameelements(followentry) //function add names array         {              allsessionfollows.push(followentry.user.name);          }          jsondata.follows.foreach(pushnameelements); //iterate through followers          storecurrentfollows(offset+100); //rerun operation, incrementing offset 100      };       if(offset > 300){ return }     else     {         //running jsonp function above, passing in url , setting callback         pulljsonp('https://api.twitch.tv/kraken/channels/' + channelname + '/follows?direction=desc&limit=100&offset=' + offset + '&callback=pullstartsessionfollows');      }   }   storecurrentfollows(0); 

so question this, whenever run operation items in order returns error > uncaught referenceerror: pullstartsessionfollows not defined in console. doesn't work unless move pullstartsessionfollows function global scope. don't understand why thinks undefined though instantiated before run pulljsonp() function. don't want move because i'll have pass in offset 2 different functions few other issues. or insight appreciated. thank in advance!

it doesn't work unless move pullstartsessionfollows function global scope.

correct, jsonp callbacks must global functions. it's intrinsic way jsonp works.

what don't understand why thinks undefined though instantiated before run pulljsonp() function.

because creates function within function (storecurrentfollows), not global. way jsonp works requires function global.

you can have pullstartsessionfollows function exist while have pending jsonp call, declaring global variable:

var pullstartsessionfollows; 

...and assigning within storecurrentfollows:

pullstartsessionfollows = function() { /* ... */ }; 

you can remove when callback has done work:

pullstartsessionfollows = undefined; 

why must callback function global? because way jsonp works adding script element page, though had written in html:

<script src="http://example.com/get?callback=pullstartsessionfollows"></script> 

...and response looks like:

pullstartsessionfollows({/*...json here...*/}) 

that requires global function.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -