2012-03-01 60 views
1
的excution流動
var total = 0; 

setTimeout(function(){ 
    total = 5000; 
},5000); 
pa = (total*40)/100; 
console.log("Pa :"+pa); 

輸出:我如何處理的NodeJS

Pa : 0 

如何獲得年利率應有的價值。我認爲回調函數..... 但不知道其詳細信息請幫我 [信息:我用定時器在上面的例子中,因爲我想從另一個函數來獲得一定的價值,它需要5秒鐘,返回] 在此先感謝...

回答

0

會強烈建議你看一看在async模塊像this.I使用本寫了一些示例代碼,正如你提到的,它返回2000

var async = require ('async'); 

var total = 0; 

async.series([ 
    function(callback){ 
     // do some stuff ... 
     setTimeout(function(){ 
      total = 5000; 
      callback(null); 
     },5000); 

    }, 
    function(callback){ 
     pa = (total*40)/100; 
     callback(null); 
    }, 
], 
//Now the other tasks are completed. 
function(err, results){ 
    console.log("Pa :"+pa); 
}); 
+0

謝謝你救了我...... – 2012-03-01 13:02:14

+0

不要爲這麼簡單的東西-.-使用異步 – Raynos 2012-03-01 14:15:10

0

您使用回調

var total = 0 

setTimeout(doLater, 5000) 

function doLater() { 
    total = 5000 
    next() 
} 

function next() { 
    pa = (total*40)/100; 
    console.log("Pa :"+pa); 
}