2017-08-02 16 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(); 
} 
}) 

此代码未经测试,但尝试过。

相关问题