First, Concept introduction
CallBack: "Callback". In spring, excellent framework callbacks are ubiquitous, and callbacks are used in many scenarios, such as SWT event monitoring, Netty, and so on. Its main role is to improve the efficiency of the program execution, a piece of code does not have to wait until another piece of code execution end before continuing to run. In JavaScript is no exception.
Second, JS callback syntax
Transfer function as callback
function (Ag1,ag2...,callback) {
Business logic Code
}
JS Code study.js
function (str, callback) { printstr (str); var res = test_callback (); Callback (res);} function printstr (str) { alert (str);} function Test_callback () { return "callback Test";}
HTML code studyjs.html
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8" /></Head><Body> <Buttononclick= "Test_callback ()">Test</Button></Body><Scriptsrc= "Js/study/study.js"type= "Text/javascript"></Script><Script>mytest ("come on", function(data) {Console.log (data); })</Script></HTML>
Analysis: Open studyjs.html, the MyTest () method is executed. After execution, go back and callback the business-defined callback function, such as
function (data) { console.log (data);
}
So the console will output "refueling"!!!
[Front-end JS Learning notes] JavaScript CallBack