简体   繁体   English

使用jQuery在函数执行完成后如何调用函数以及从上一个函数调用另一个函数

[英]Using jquery how to call a function and call another function from the previous when function execution is completed

I have to perform a task based on jquery. 我必须执行基于jquery的任务。 When the page loads, the first jquery function will be called. 页面加载时,将调用第一个jquery函数。 Once the first function completes successfully, then it should automatically call the next function. 第一个函数成功完成后,便应自动调用下一个函数。

If the function is actually a function then just call it functionA() as per good ol' JavaScript. 如果该函数实际上是一个函数,则可以按照良好的JavaScript调用它functionA() If it is an event then use Jquery trigger see http://api.jquery.com/trigger/ 如果是事件,请使用Jquery trigger请参阅http://api.jquery.com/trigger/

if it is possible then chaining the functions might also be a good idea.as in FunctionA().FunctionB(); 如果可能的话,将函数链接起来也是一个好主意。例如FunctionA().FunctionB();

you can 'pass' callback function to other functions. 您可以将回调函数“传递”到其他函数。 see example: 参见示例:

function one(callback) {
    // do stuff ...
    // do stuff ...
    // do stuff ...

    if (typeof(callback) === 'function') 
        callback();
}

function two() {
}

now, declare document ready and call function one . 现在,声明文档准备就绪并调用函数one when it completes, we'll call function two . 完成后,我们将调用函数two

$(document).ready(function(){
    one(function(){
        // code after 'one' ended
        two();
    });
});

that's the general idea. 这是一般的想法。 hope that helps. 希望能有所帮助。

Here is a working demo JSFIDDLE 这是一个工作示例JSFIDDLE

JS: JS:

function func_one()
{
    alert('func_one');           
    alert('Now call func_two');   
    func_two();
}

function func_two()
{
    alert('func_two');    
}
//jquery Function block that will be executed on page load
$(document).ready(function(){
    //call the function one
    func_one();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM