Javascript ManualReset事件 [英] Javascript ManualReset event
问题描述
我有一个JavaScript API,该API的函数x
需要一些参数和一个回调函数.我有什么办法可以像在C#中使用ManualResetEvent
一样,在调用它的函数中等待回调吗?
I have a JavaScript API that has a function x
that takes some parameters and a callback function. Is there a way I can wait for the callback in the function that calls it almost the same way ManualResetEvent
works in C#?
推荐答案
JavaScript与ManualResetEvent类似.实际上,大多数浏览器都在单个线程中运行JavaScript(Firefox确实引入了WebWorkers,但它特定于Firefox,尚未广泛使用.).
JavaScript has nothing like ManualResetEvent. In fact, JavaScript is run in a single thread by most browsers [Firefox did introduce WebWorkers but it's specific to Firefox and not in widespread use yet.].
如果API方法采用回调函数参数,则它将在方法执行期间调用它,或者在完成某项操作时结束时调用它.因此,您无需等待"自己.
If an API method takes a callback function argument, it will either call it during the method execution OR at the end when it is finished doing something. So there is no need for you to 'wait' yourself.
例如在jQuery中,动画方法采用回调函数,动画结束时会调用该函数.
e.g. in jQuery, animate methods take callback function which is called when the animation is over.
$('#myDiv').slideUp(3000, function() { alert('I will be called after slideUp animation is over');});
如果您不能提供更多有关API方法调用以及您要执行的操作的详细信息,我们可以为您提供帮助.
If you could give little more details about your API method call and what you are trying to do, we can help you out.
这篇关于Javascript ManualReset事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!