1 回答

TA貢獻1853條經驗 獲得超9個贊
您可以使用標志來檢查時差。用戶單擊提交按鈕后,您可以將時間存儲在變量中。如果用戶再次單擊提交按鈕,請檢查當前時間和上次提交時間(存儲在變量中)之間的差異。如果時間差小于 60 秒,則顯示警報消息,否則執行發布請求。
var submissionTime = undefined;
form.addEventListener('submit', (e) => {
e.preventDefault();
var flag = true;
if(submissionTime) {
var milliseconds = new Date().getTime() - submissionTime.getTime();
var seconds = Math.abs(milliseconds / 1000);
if(seconds < 61) {
flag = false;
// show the alert
}
}
if(flag) {
submissionTime = new Date();
const choice = document.querySelector('input[name=os]:checked').value;
const data = {os:choice}
fetch('localhost:3000/poll', {
method: 'post',
body: JSON.stringify(data),
headers: new Headers({
'Content-Type': 'application/json'
})
})
.then(res => res.json())
.then(data => console.log(data))
// alert("Danke für die Abstimmung")
.catch(err => console.log(err));
}
});
添加回答
舉報