我對NodeJS和JavaScript一般都很陌生。這裏是我的腳本:爲什麼Nodejs的callback()無法訪問回調範圍之外的變量?
var fs = require('fs') ;
var temp = "???";
var test = function(){
fs.readdir("./", function(err,result){
temp = result; // i change the temp's value
console.log("inter result ....."+temp); // temp's value changed
setTimeout(pr,1000,"inter setTimeout: "+temp); // temp's value changed
});
}
var pr = function (str){
console.log("Print str: "+ str);
} ;
test();
setTimeout(pr,1000,"Out setTimeout print: "+temp); // Why here temp's value not change???
我怎樣才能更改爲回調以外的var temp的值?
的'readdir'回調可能執行*之後*了'setTimeout'回調... – 2012-01-31 23:17:26