为什么 Nodejs 回调()无法访问回调范围之外的变量?

发布于 2024-12-29 22:10:18 字数 609 浏览 1 评论 0原文

总的来说,我对 NodeJS 和 JavaScript 还很陌生。这是我的脚本:

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 的值?

I am fairly new to NodeJS and to JavaScript in general. Here is my script:

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???

How can I change to the var temp’s value outside the callback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2025-01-05 22:10:18
setTimeout(pr,1000,"Out setTimeout print: "+temp);

相同

var str = "Out setTimeout print: " + temp;
setTimeout(pr, 1000, str);

与此时 temp 仍然是 "???" 。你必须使用

setTimeout(function() {
  pr("Out setTimeout print: "+temp);
}, 1000);
setTimeout(pr,1000,"Out setTimeout print: "+temp);

is the same as

var str = "Out setTimeout print: " + temp;
setTimeout(pr, 1000, str);

At this point of time temp still is "???". You have to use

setTimeout(function() {
  pr("Out setTimeout print: "+temp);
}, 1000);
哥,最终变帅啦 2025-01-05 22:10:18

日志语句在控制台中显示的顺序是什么?

我不喜欢 node.js,但我希望在“inter”之前看到“Out”,因为我猜测 fs.readdir() 函数是异步的,并且回调您提供给它的函数只有在您在代码的最后一行中调用 setTimeout() 之后才会执行,此时 temp 还没有执行却被改变了。

也就是说,我期望您的代码的执行顺序是:

  1. define fs
  2. definetempset to???
  3. definetest 函数
  4. 定义 pr 函数
  5. 调用 test() 函数
  6. test() 中调用 fs.readdir() 但立即从 test() 返回,而回调尚未执行
  7. setTimeout(pr,1000,"Out setTimeout print: "+temp); (此时 temp 的值 - 仍然是“???” - 成为setTimeout 将在一秒内传递给 pr 的字符串)
  8. 执行来自 fs.readdir() 的回调,然后才执行 temp换衣服吧。 “inter”超时已设置。

What order do the log statements appear in your console?

I'm not into node.js, but I would expect to see the "Out" one before the "inter" ones because I would guess the fs.readdir() function is asynchronous and that the callback function that you provide to it will not be executed until after you've already made the call to setTimeout() in the last line of your code at which point temp has not yet been changed.

That is, the sequence of execution I would expect from your code is:

  1. define fs
  2. define temp set to ???
  3. define test function
  4. define pr function
  5. call test() function
  6. within test() call the fs.readdir() but then return immediately from test() without the callback having been executed yet
  7. setTimeout(pr,1000,"Out setTimeout print: "+temp); (where the value of temp at that moment - still "???" - becomes part of the string that setTimeout will pass to pr in one second's time)
  8. the callback from fs.readdir() is executed, and only then does temp get changed. The "inter" timeout gets set.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文