简体   繁体   中英

How to know that a callback function has fired?

How can I know that a callback event has fired? I have to perform an activity after the callback event has fired, should I put the code inside the callback function or is there some other way to know when the callback has fired? Even if I put the code of the activity after the statement making the javascript request I cant be sure that the code of the activity will be executed after the callback has fired?

You have to put it in the callback function. You can do this indirectly (eg the callback function calls another).

How to know that a callback function has fired?

You can simply put an alert in the callback function to know whether or not it has fired.

Code inside the callback function will fire anyway.

How can I know that a callback event has fired?

do something at the end of the callback. like calling the function that should be invoked after the callback.

I have to perform an activity after the callback event has fired, should I put the code inside the callback function or is there some other way to know when the callback has fired?

There is no other way to determine if a callback has been fired.

Even if I put the code of the activity after the statement making the javascript request I cant be sure that the code of the activity will be executed after the callback has fired?

by "javascript request" do you mean "ajax request". If so I would advise at doing it this way because it most probably will be fired before the callback. At most ajax requests are per definition asynchronous ( A synchronous J avaScript a nd X ML = AJAX). And because of that the script won't wait for the response of the request to continue with itself. That is why you should use callbacks. Callbacks make sure that some code is called after something specified had happened.

If you had a callback function like following:

setup(data, namespaces, eventHandler) {
    $(this).resize(eventHandler);
});

You can pull your callback into another callback and take control before that function is called.

setup(data, namespaces, eventHandler) {
    $(this).resize(() => {
        console.log("calling eventHandler()")
        eventHandler();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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