0
`var req = http.request(options, function(res) 
       { 
              hh=res.headers["last-modified"]; /* hh is global variable */

       });req.end();
      /*print the last modified time of file that stored in hh
       console.log(hh);

req.end 后获取 hh 中的值后如何使用 hh?

4

1 回答 1

0

请做一些关于异步代码的教程。您不了解执行顺序,我在下面强调了这一点。

//this line executes at time T1.1
var req = http.request(options, function(res) {
  //This line executes at time T2.1, AFTER T1.1 and T1.2 
  hh=res.headers["last-modified"]; /* hh is global variable */
  //This line executes at time T2.2 and hh IS AVAILABLE NOW
  console.log(hh);
});
//This line executes at time T1.2
//hh is NOT SET YET. CANNOT USE IT HERE. This is what async means.
req.end();
于 2013-09-02T20:08:54.767 回答