js custom callback function_javascript skills
Background Analysis
First look at a piece of js code. When adding, first determine whether it exists through an asynchronous request. If it does not exist, add it:
function add(url,data) { var isExited = isExited(data); if(!isExited){ addRequest(url, data); } }
When I add a piece of data, I first judge whether it exists in the database (of course, if the front-end and back-end are completely separated, the front-end should not judge the business logic, the front-end should only be used to display the data), first , the request of isExited() is an ajax request implementation. This is asynchronous. Obviously, the interface is likely to execute the following function when no result is returned (usually yes), making the value of isExited undefined. , this is obviously not what you want. If you want to achieve similar functions, you can use a callback function. A case is introduced below.
The process is as follows
The front-end jsp interface is as follows:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>JS回调函数案例</title> <!-- Bootstrap --> <link href="<c:url value='/lib/bootstrap/css/bootstrap.min.css'/>" rel="stylesheet"> <script type="text/javascript"> /** * 删除的请求 */ function supplierDelete(element) { var id = element.parentNode.parentNode.cells[0].innerHTML; modalDeleteRequest('${pageContext.request.contextPath}/oms/supplier/remove/', id); } </script> </head> <body> <!-- 顶部导航 --> <div class="navbar navbar-inverse navbar-fixed-top" role="navigation" id="menu-nav"> </div> <div class="container partner-table-container textFont"> <table class="table table-striped detailTableSet"> <caption><h2>JS回调函数案例</h2></caption> <br> <tr class="table-hover form-horizontal"> <td class="info">123</td> <td class="info">123</td> <td class="info">123</td> <td class="info">123</td> <td class="info">123</td> </tr> <tr> <td>123</td> <td>123</td> <td>123</td> <td>123</td> <td>123</td> <td> <a onclick="supplierUpdate(this)">修改</a> <a onclick="supplierDelete(this)">删除</a> </td> </tr> </table> </div> <!--显示成功失败的modal--> <%@include file="/modal-custom.jsp" %> <script src="<c:url value='/lib/jquery-1.8.3.min.js'/>"></script> <script src="<c:url value='/lib/bootstrap/js/bootstrap.min.js'/>"></script> <script type="text/javascript" src="<c:url value='/js/modal-operate.js'/>"></script> </body> </html>
The main js code is as follows:
<script type="text/javascript"> /** * 删除的请求 */ function supplierDelete(element) { var id = element.parentNode.parentNode.cells[0].innerHTML; modalDeleteRequest('${pageContext.request.contextPath}/oms/supplier/remove/', id); } </script>
Here is when the button is clicked to delete, but I want to pop up a confirmation deletion dialog box. If the confirmation is selected after the pop-up, the specific deletion method will be called. There is also a modal box (bootstrap) referenced here. Frame), mainly used to display pop-up box information, the code is as follows:
<%@ page language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- 模态框(Modal) --> <div class="modal fade" id="modal-result" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel"> 信息 </h4> </div> <div class="modal-body" id="modal-add-result-text"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭 </button> </div> </div> <!-- /.modal-content --> </div> <!-- /.modal --> </div>
The following is today’s protagonist:
/** * 删除请求的操作 * @param url 删除请求的url * @param id 删除的id */ function modalDeleteRequest(url, id) { confirmIsDelete(url, id, deleteRequest); } /** * 在删除警告框确认之后调用的回调函数 * @param url * @param id */ function deleteRequest(url, id) { $.get(url + id, function (result) { $("#modal-add-result-text").text(result.msg); $("#modal-result").modal('show'); }, "json"); } /** * 弹出对话框确认是否删除 * @param url 删除请求的url * @param id 删除请求的id * @param callback 回调函数,在最后的时候需要进行回调的函数 */ function confirmIsDelete(url, id, callback) { var confirmDeleteDialog = $('<div class="modal fade"><div class="modal-dialog">' + '<div class="modal-content"><div class="modal-header"><button type="button" class="close" ' + 'data-dismiss="modal" aria-hidden="true">×</button>' + '<h4 class="modal-title">确认删除</h4></div><div class="modal-body">' + '<div class="alert alert-warning">确认要删除吗?删除之后无法恢复哦!</div></div><div class="modal-footer">' + '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>' + '<button type="button" class="btn btn-success" id="deleteOK">删除</button></div></div>' + '</div></div>'); confirmDeleteDialog.modal({ keyboard: false }).on({ 'hidden.bs.modal': function () { $(this).remove(); } }); var deleteConfirm = confirmDeleteDialog.find('#deleteOK'); deleteConfirm.on('click', function () { confirmDeleteDialog.modal('hide'); //隐藏dialog //需要回调的函数 callback(deleteRequest(url, id)); }); }
Write picture description here
Write picture description here
Since there is a lot of code above, let’s look at a simple framework below:
/** * 回调函数测试方法 * * @param callback * 回调的方法 */ function testCallback(callback) { alert('come in!'); callback(); } /** * 被回调的函数 */ function a() { alert('a'); } /** * 开始测试方法 */ function start() { testCallback(a); }
This is the end of the callback. I hope it will be helpful for everyone to learn. The editor also has a deeper understanding of js custom callback functions.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

The writing methods of java callback function are: 1. Interface callback, define an interface, which contains a callback method, use the interface as a parameter where the callback needs to be triggered, and call the callback method at the appropriate time; 2. Anonymous inner class callback , you can use anonymous inner classes to implement callback functions to avoid creating additional implementation classes; 3. Lambda expression callbacks. In Java 8 and above, you can use Lambda expressions to simplify the writing of callback functions.

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
