javascript - jquery making local variable in function global -
i have function populates currsong variable data. problem variable currsong available inside function , need access globally.
// single audio url echonest.artist(artist).audio( function(audiocollection) { var currsong = audiocollection.data.audio[0].url; return currsong; });
thank you!
to declare global variable, can remove var
:
currsong = audiocollection.data.audio[0].url;
i'm not sure if global variable solution whatever you're trying do, though. people suggest avoid them reason.
edit
an example.
note, variable undefined before function executed first time. in code, pass audio
, don't invoke it.
edit2
tgr notes, can declare global variable explicitly: window.currsong = ...
. there's no functional difference, improves code quality.
Comments
Post a Comment