Home > Web Front-end > JS Tutorial > How to customize a callback function in jQuery

How to customize a callback function in jQuery

伊谢尔伦
Release: 2017-06-16 14:59:59
Original
2279 people have browsed it

First of all, from the literal translation of callback "callback", we can understand that this is a mechanism for function calls.

When we encounter a noun, we may first search on Baidu and Google to see how the official explanation is

The following is the definition of callback from Wikipedia:

a callback is a piece of executable code that is passed as an argument to other code which is expected to call back (execute) the argument at some convenient time

Hard translation: A callback function is an executable code segment that is used as a parameter of another function. This code segment is executed at a convenient time.

Popular explanation: Treat function f2 as a The parameters are passed to function f1 and f2 is executed at the appropriate time in f1 (I use f1, f2 in all the examples below)

So we can get a callback function pattern like this

function f1(f2) {    //f1要执行的一些代码
    if (f2 && typeof(f2) === "function") { //对f2做判断是否存在并且是一个函数        f2();
    }
}
Copy after login

It should be noted here that f2 in the two parameters is a pointer to the function f2, so f2 cannot be followed by brackets

, and f2 inside f1 must be followed by brackets because at this time we need to call and execute f2, so we must write f2()

Let’s instantiate this pattern and see its execution results

声明函数function f1(f2) {
alert("我是f1");if (f2 && typeof(f2) === "function") { //写上判断是个好习惯f2()
}
}
执行
f1(function() {
alert("我是f2");
})
结果://"我是f1",“我是f2”
Copy after login

Let’s look at the entire function execution process. When calling f1(function(){alert("I am f2 ");})

First pass an anonymous function to f1(), and this anonymous function is the parameter f2 in the declared function. The javascript program is executed from top to bottom. Alert("I It's f1"); and then executed f2();

Can we write it like this?

function f1(f2) {    alert("我是f1");    if (f2 && typeof(f2) === "function") {  
        f2()
    }    alert("我又是f1 哈哈");
}
执行
f1(function(){
    alert("我是f2");
})//结果:我是f1 ,我是f2,我又是1 哈哈
Copy after login

Looking at the definition of callback, we can make the callback execute when we want to Execution does not affect the execution flow of f1 itself

But for encapsulation and beauty, most of us will write it like this

function f1(f2){ 
if (f2 && typeof(f2) === "function") {  
settimeOut(function(){f2()},1000) ;//f1执行1s后f2执行}
}function f2(){/**/}
f1(f2);
Copy after login

In fact, the callback function is not that complicated. In js, you can treat function as a Ordinary parameters. As long as () is added after it, it means calling this function.

Look at the following example again

$(function(){
    function funcname(param){
        //do something
         
        //callback
        param.callback();    
    }
 
    //调用
    funcname({
        callback:function(){
            alert('callback do'); 
        }   
    });      
});
Copy after login

It can also be seen from the above example that in fact, every time a function is defined, the function is added to the stack of the container, and the index is the function name. The default is under window, so you can throw the string there, and you can also call this callback function through the string under window.

If you have parameters, you can use the above method.

For example, an example in jQuery

$("#div1").fadeOut("fast",functin(){
    $("#div2").fadeIn("slow");
})
Copy after login

Let #div be quickly hidden and #div2 gradually displayed. There are a large number of callback functions in jQuery

and there is a dedicated There is a method callbacks to manage

jQuery.Callbacks = function( options ) {...}
Copy after login

Finally, let me talk about the callbacks I have recently used in the project

The function of hybrid app is probably that I send a request to ios and ios returns the json to me. Data then I parse and insert the returned data into the web page

//首先我写一个javascript和ios通信callback函数(简化)getData(callback){
settimeOut(function(){callback(iso_return)},100);
}//然后我向ios发送一个请求function getSinersReuest(){window.location.href="vvmusic://....callback=getSinersData"}/*然后ios截获url中的callback执行getData(getSinersData)返回给我数据;我的getSinersData是我想把ios返回的json插入到页面显示8*/getSinersData(iso_return){
.........
}//当页面加载的时候调用window.onload=function(){
getSinersReuest();
}
Copy after login

The above is the detailed content of How to customize a callback function in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template