如何在javascript中为函数添加回调

时间:2021-05-13 16:55:38

I have two javascript functions

我有两个javascript函数

function one () {
   do something long... like writing jpgfile on disk
}

function two () {
   do something fast... like show the file
}

I call it (in jQuery) like this

我这样称呼它(在jQuery中)

 one ();
 two ();

Because function two needs the link file from function one, i need to be sure the execution is completed... so getting the function two in the callback of function one should be the trick.. but how to do that ?

因为函数二需要来自函数一的链接文件,我需要确保执行完成...所以在函数one的回调中获取函数2应该是技巧..但是如何做到这一点?

note : I did put an alert ('aaa') between those two functions to let function one complete, and it worked fine... when the alert is commented (removed) nothing works anymore !

注意:我确实在这两个函数之间放了一个警报('aaa')让函数一个完成,它工作得很好......当警报被注释(删除)时,没有任何工作了!

4 个解决方案

#1


43  

You only need to use a callback if you are doing something asynchronous, otherwise it doesn't matter how long something takes, the next function won't run until the first has finished.

如果你正在做异步的事情,你只需要使用一个回调,否则无论事情需要多长时间,下一个函数在第一个函数完成之前都不会运行。

A callback is just passing a function as an argument, and then calling it when done.

回调只是将函数作为参数传递,然后在完成时调用它。

function one (callback) {
   do something long... like writing jpgfile on disk
   callback();
}

function two () {
   do something fast... like show the file
}

one(two);

Obviously, if you are doing something asynchronous, then you need something that will tell you when it is finished (such as an event firing).

显然,如果你正在做一些异步的事情,那么你需要一些可以在完成时告诉你的事情(例如事件发生)。

#2


13  

Simple:

简单:

function one (callback) {
   do something long... like writing jpgfile on disk

    if(callback) callback();
}

function two () {
   do something fast... like show the file
}

one(two);

#3


4  

Try this,

尝试这个,

$.when($.ajax(fuction1())).then(function () {

fuction2;

});

Here fuction1 is your first function to call, and fuction2 is your second function.

fuction1是你的第一个调用函数,fuction2是你的第二个函数。

#4


0  

I think it's easy if the browser wait for the process inside "one()" to be done before execute the next line of command. The iceberg hit titanic cause it doesn't wait. Then executing this:

我认为如果浏览器在执行下一行命令之前等待“one()”内的进程,这很容易。冰山遭遇巨大因素,因为它不等待。然后执行:

one(two) // while two is the callBack parameter

is nothing different from:

没有什么不同于:

one()
two()

I suggest using a setInterval.

我建议使用setInterval。

function one(){
    //--- Write the file to disk
    //.....................
}

function runTwo(){
    if (check_the_written_file_existence){
         clearInterval(t)
         two();
    }
}
var t = setInterval("runTwo()",500)

The most important point is that if there's an event fires when the "long process" in function "one()" has done, you just need to bind function two to that event. Unless, you must check the result by someway every span of time until it's really done.

最重要的一点是,如果在“one()”函数中的“长进程”发生时触发事件,则只需将函数二绑定到该事件即可。除非,你必须在每个时间跨度检查结果,直到它真的完成。

#1


43  

You only need to use a callback if you are doing something asynchronous, otherwise it doesn't matter how long something takes, the next function won't run until the first has finished.

如果你正在做异步的事情,你只需要使用一个回调,否则无论事情需要多长时间,下一个函数在第一个函数完成之前都不会运行。

A callback is just passing a function as an argument, and then calling it when done.

回调只是将函数作为参数传递,然后在完成时调用它。

function one (callback) {
   do something long... like writing jpgfile on disk
   callback();
}

function two () {
   do something fast... like show the file
}

one(two);

Obviously, if you are doing something asynchronous, then you need something that will tell you when it is finished (such as an event firing).

显然,如果你正在做一些异步的事情,那么你需要一些可以在完成时告诉你的事情(例如事件发生)。

#2


13  

Simple:

简单:

function one (callback) {
   do something long... like writing jpgfile on disk

    if(callback) callback();
}

function two () {
   do something fast... like show the file
}

one(two);

#3


4  

Try this,

尝试这个,

$.when($.ajax(fuction1())).then(function () {

fuction2;

});

Here fuction1 is your first function to call, and fuction2 is your second function.

fuction1是你的第一个调用函数,fuction2是你的第二个函数。

#4


0  

I think it's easy if the browser wait for the process inside "one()" to be done before execute the next line of command. The iceberg hit titanic cause it doesn't wait. Then executing this:

我认为如果浏览器在执行下一行命令之前等待“one()”内的进程,这很容易。冰山遭遇巨大因素,因为它不等待。然后执行:

one(two) // while two is the callBack parameter

is nothing different from:

没有什么不同于:

one()
two()

I suggest using a setInterval.

我建议使用setInterval。

function one(){
    //--- Write the file to disk
    //.....................
}

function runTwo(){
    if (check_the_written_file_existence){
         clearInterval(t)
         two();
    }
}
var t = setInterval("runTwo()",500)

The most important point is that if there's an event fires when the "long process" in function "one()" has done, you just need to bind function two to that event. Unless, you must check the result by someway every span of time until it's really done.

最重要的一点是,如果在“one()”函数中的“长进程”发生时触发事件,则只需将函数二绑定到该事件即可。除非,你必须在每个时间跨度检查结果,直到它真的完成。