- Mastering JavaScript Functional Programming
- Federico Kereki
- 177字
- 2021-07-02 22:41:09
The problem - do something only once
Let's consider a simple, but common situation. You have developed an e-commerce site: the user can fill their shopping cart, and at the end, they must click on a BILL ME button, so their credit card will be charged. However, the user shouldn't click twice (or more) or they would be billed several times.
The HTML part of your application might have something like this, somewhere:
<button id="billButton" onclick="billTheUser(some, sales, data)">Bill me</button>
And, among the scripts you'd have something similar to this:
function billTheUser(some, sales, data) {
window.alert("Billing the user...");
// actually bill the user
}
Assigning the events handler directly in HTML, the way I did it, isn't recommended. Rather, in unobtrusive fashion, you should assign the handler through code. So... Do as I say, not as I do!
This is a very barebones explanation of the problem and your web page, but it's enough for our purposes. Let's now get to think about ways of avoiding repeated clicks on that button... How can we manage to avoid the user clicking more than once?
- The DevOps 2.3 Toolkit
- PWA入門與實(shí)踐
- Mastering Natural Language Processing with Python
- 看透JavaScript:原理、方法與實(shí)踐
- Elastic Stack應(yīng)用寶典
- 64位匯編語(yǔ)言的編程藝術(shù)
- Java深入解析:透析Java本質(zhì)的36個(gè)話題
- C#應(yīng)用程序設(shè)計(jì)教程
- Go語(yǔ)言精進(jìn)之路:從新手到高手的編程思想、方法和技巧(1)
- Python極簡(jiǎn)講義:一本書入門數(shù)據(jù)分析與機(jī)器學(xué)習(xí)
- Citrix XenServer企業(yè)運(yùn)維實(shí)戰(zhàn)
- Maker基地嘉年華:玩轉(zhuǎn)樂(lè)動(dòng)魔盒學(xué)Scratch
- OpenCV 3計(jì)算機(jī)視覺:Python語(yǔ)言實(shí)現(xiàn)(原書第2版)
- Microsoft Dynamics GP 2013 Cookbook
- LabVIEW數(shù)據(jù)采集(第2版)