既然是根據當前的時間,那么我們第一步就是獲取當前時間,然后拼接一個隨機數,這樣便實現了隨機生成一個流水號,注意由于要求每個流水號都是獨一無二的,生成后提交的過程中還是需要驗證當前的流水號是否存在,如果存在則需要提示用戶或者再次生成。
js;">// 根據當前時間和隨機數生成流水號 randomNumber() { const now = new Date() let month = now.getMonth() + 1 let day = now.getDate() let hour = now.getHours() let minutes = now.getMinutes() let seconds = now.getSeconds() month = this.setTimeDateFmt(month) hour = this.setTimeDateFmt(hour) minutes = this.setTimeDateFmt(minutes) seconds = this.setTimeDateFmt(seconds) return now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 89 + 100)).toString() }
總結
以上所述是小編給大家介紹的用JS實現根據當前時間隨機生成流水號或者訂單號,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答