- 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?
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- Learn TypeScript 3 by Building Web Applications
- C#程序設計實訓指導書
- Python編程:從入門到實踐
- 硅谷Python工程師面試指南:數據結構、算法與系統設計
- UVM實戰
- Unity 2017 Mobile Game Development
- C# Multithreaded and Parallel Programming
- Mastering Elasticsearch(Second Edition)
- MINECRAFT編程:使用Python語言玩轉我的世界
- Bootstrap for Rails
- 零代碼實戰:企業級應用搭建與案例詳解
- JavaEE架構與程序設計
- 征服C指針(第2版)