2013-02-15 49 views
1

我是一個總的新手,如果涉及到回調,所以我嘗試了一些Google搜索。但是代碼(有人在論壇上給我的)與我遇到的回調示例非常不同。如何檢查此回調的回報

任何人都可以請詳細說明我如何得到這個結果的真或假嗎?

exports.testGPS = function(_callback) { 
    Ti.Geolocation.purpose = "Recieve User Location"; 
    Ti.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; 
    if (_callback && typeof(_callback)==='function') { 
     Ti.Geolocation.getCurrentPosition(function(e) { 
      _callback ((e.error) ? false: true); 
     }); 
    } 
} 

這裏是我的電話:

functions.testGPS(function() { 
    //If true/false 
}); 

回答

3

聲明一個參數爲你的函數:

functions.testGPS(function (thereWasAnError) { 
    if (thereWasAnError) { 
     // do stuff 
    } 
}); 
+0

太容易了。我是個白癡。非常感謝! – CaptainCarl 2013-02-15 13:47:28

+0

不是一個白癡,而是一個新手。就像我們所有人一樣;) – 2013-02-15 13:53:07

2

回調返回作爲參數的函數的結果。添加返回的變量,並檢查它

functions.testGPS(function (result) { 
    if(result) 
    { 
     //TRUE 
    } 
    else 
    { 
     //FALSE 
    } 
}); 
1

創建一個函數說FetchResult()

function fetchResult(result) 
{ 
    alert(result); 
} 

呼叫傳遞迴調函數,

exports.testGPS(fetchResult) 

自動,其結果將被傳遞到你的函數調用fetchResult。