如何从a_var
开始访问setTimeout
中的someFunction
?
感谢。
function someFunction(){
(function why(){
setTimeout(function(){
var a_var='help I wanna get out!';
return a_var;//<-useless?
}, 25);
})();
};
答案 0 :(得分:1)
您必须在更高的范围内声明a_var,如下所示:
var a_var = 'I can help from here';
function someFunction(){
setTimeout(function(){
a_var = "help I wanna get out!";
}, 25);
}
someFunction();
console.log(a_var); // logs 'I can help from here'
setTimeout(function(){
console.log(a_var);
}, 30); // logs 'help I wanna get out!';