2017-08-02 50 views
-3

我是新來的JavaScript世界,到目前爲止我只是在前端做一個簡單的東西的JavaScript,這是我第一次嘗試使用JavaScript做一個邏輯程序,你可以幫我嗎?完成一個後調用另一個函數,並使結果成爲一個變量

讓我們說我有3個功能,現在通過AJAX調用的函數,函數B和C.功能

調用JSON和現在,我想讓函數的準備當一個頁面的onload,然後經過函數A完成獲取JSON,它將調用函數B運行,等等,直到函數C完成調用JSON。

如果Function以某種方式得到一個錯誤/無法獲得JSON,他們將停止並且不繼續該進程(調用其他函數)。 I.e:
如果函數A失敗,它將停止在函數A中,而不是繼續它。如果函數A成功,那麼它將調用函數B,並且如果函數B失敗,它將在函數B中停止,而不是繼續它。等到功能C完成。

而在這之後的所有,我想我需要的結果(JSON)是通過AJAX在功能上,函數B和函數C叫成了瓦爾A,無功B和C.瓦爾

難道用Jscript可以實現這一點嗎?

+1

[在另一個函數完成後執行jquery函數]可能的重複(https://stackoverflow.com/questions/19674380/execute-jquery-function-after-another-function-completes) –

+0

歡迎來到Stack Overflow!請參考[參考],環顧四周,並閱讀[幫助],尤其是[*我如何提出一個好問題?](/ help /如何問)代碼是1024字。嘗試通過徹底的研究,調試等來完成您所描述的任務。**如果您遇到問題,請使用代碼的[mcve]發表問題,說明您遇到什麼具體問題以及您擁有什麼做診斷/修復它,人們會很樂意提供幫助。 –

回答

1

用這麼少的具體信息寫出答案真的很難,但我會嘗試。

我認爲您需要的是promises,因爲它們允許您鏈接多個異步操作。只要出現錯誤,鏈就會中斷,導致調用錯誤處理程序(您可以選擇指定)。

讓我們來定義加載文件a.json功能functionA

function functionA() { 
    return new Promise(function (resolve, reject) { 
    const xhr = new XMLHttpRequest(); 
    xhr.open('GET', 'a.json'); 
    xhr.onload = function() { resolve(xhr.response); }; 
    xhr.onerror = function() { reject('Could not load a.json'); }; 
    }); 
} 

使用此功能是這樣的:

functionA().then(function (response) { 
    // this function is called as soon as the contents of the file are loaded 
    // response contains the contents of the file 
}).catch(function (errorMessage) { 
    // this function is called whenever an error occurs 
    // errorMessage contains the error message 
}); 

比方說,你定義以類似的方式功能functionBfunctionCfunctionA 。然後,您可以使用這樣的:

let contentsOfFileA = ''; 
let contentsOfFileB = ''; 
let contentsOfFileC = ''; 
functionA() 
    .then(fileA => { 
    contentsOfFileA = fileA; 
    return functionB(); 
    }).then(fileB => { 
    contentsOfFileB = fileB; 
    return functionC(); 
    }).then(fileC => { 
    contentsOfFileC = fileC; 
    // now, all files are loaded and you can use the variables 
    // contentsOfFileA, contentsOfFileB and contentsOfFileC 
    }).catch(error => console.log('An error occurred:', error)); 

的片段上方包含非常冗餘代碼。使用Promise.all,可以縮短它:

Promise.all(functionA(), functionB(), functionC()) 
    .then([fileA, fileB, fileC] => { 
    // do something with the three files' contents 
    }).catch(error => console.log('An error occurred:', error)); 

當然,什麼functionAfunctionBfunctionC正在做的是非常微不足道的。要加載JSON文件,你還可以使用fetch API

Promise.all(['a.json', 'b.json', 'c.json'].map(file => fetch(file))) 
    .then(responses => Promise.all(responses.map(r => r.json()))) 
    .then(([fileA, fileB, fileC]) => { 
    // do something with the three files' contents 
    }).catch(error => console.log('An error occurred:', error)); 
1
function toCall(callback){ 
//do all the stuff you want to 
return callback(); 
} 

您的函數將返回回調的值。例如: :

let number = toCall(function(){ return 1; });

1

你爲什麼不繼續使用jQuery的?

$(document).ready(function() { //because you want to execute after page load. 

$.when(function a()).then(function b()); 
} 
//For Ajax 
function a(){ 
    // your code function a 
} 
function b(){ 
    // your code function b 
} 

$.ajax({ 
    url:a(), 
    success:function(){ 
    b(); 
} 
}) 

此代碼未經測試,但嘗試過。

相關問題