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(); } }
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”
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 哈哈
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);
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'); } }); });
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"); })
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 ) {...}
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(); }
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!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
