亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

NodeJS 倒計時

NodeJS 倒計時

大話西游666 2022-07-15 09:50:27
我正在調用提交按鈕并為每次點擊添加選中的值。我想添加一個倒計時并說:每 60 秒只計算一次用戶點擊。否則顯示警報。這是我的代碼:form.addEventListener('submit', (e) => {    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));    e.preventDefault();});
查看完整描述

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));

  }


});


查看完整回答
反對 回復 2022-07-15
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號