Javascript 如何在javascript中向函数添加回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7691762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a callback to a function in javascript
提问by menardmam
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 ?
因为函数二需要函数一的链接文件,我需要确保执行完成......所以在函数一的回调中获取函数二应该是诀窍......但是如何做到这一点?
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')来让一个函数完成,并且它工作正常......当警报被评论(删除)时,没有任何作用!
回答by Quentin
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 aredoing something asynchronous, then you need something that will tell you when it is finished (such as an event firing).
显然,如果你正在做一些异步的事情,那么你需要一些东西来告诉你它什么时候完成(比如一个事件触发)。
回答by Joe
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);
回答by Jinesh
Try this,
尝试这个,
$.when($.ajax(fuction1())).then(function () {
fuction2;
});
Here fuction1 is your first function to call, and fuction2 is your second function.
这里 fuction1 是您要调用的第一个函数,而 fuction2 是您要调用的第二个函数。
回答by vantrung -cuncon
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()”中的“长过程”完成时触发了一个事件,您只需要将函数二绑定到该事件。除非,您必须每隔一段时间以某种方式检查结果,直到真正完成。