How to learn jquery from zero using callback functions

  • 2020-03-30 03:00:02
  • OfStack

In C - like languages it is usually passed by function pointer/reference.

Jquery provides a similar callback function mechanism. But it's still worth mentioning how to pass the callback function correctly.

  1. Callbacks without parameters

$.get('myhtmlpage.html', myCallBack);

MyCallBack is the name of the function. Functions are the foundation of javascript. Can be passed as a reference variable.

2. Callbacks with parameters

Naturally, based on past experience, we would expect a callback with a parameter to look like this:

$.get('myhtmlpage.html', myCallBack(param1, param2));

But this will not work properly. MyCallBack (param1, param2) is executed when the statement is called, not after.

The following syntax is correct:

$.get('myhtmlpage.html', function(){
  myCallBack(param1, param2);

The callback function is passed as a function pointer and will be executed after the get operation completes.

Related articles: