javascript - JS - Set variables value in function but use variable outside of it? -
i need set variables value within function, use outside of function. can done?
$(document).ready(function() { $(window).scroll(function () { var myvar = something; }); console.log(myvar); });
yes, need first declare outside of function.
$(document).ready(function() { var myvar; $(window).scroll(function () { myvar = something; }); console.log(myvar); });
just know myvar
updated after scroll event triggered. so, console.log
log undefined
because runs before event runs , sets variable.
Comments
Post a Comment