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

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

在 keyup/keydown 事件上具有唯一超時的 jquery 多個選擇器

在 keyup/keydown 事件上具有唯一超時的 jquery 多個選擇器

千萬里不及你 2023-03-24 17:17:22
我正在嘗試創建一個自動保存輸入到幾個文本框中的輸入的功能。我在一個選擇器 ($input) 中有 3 個輸入,還有一個在 keyup/paste 和 keydown 上更新的計時器 (typingTimer)。如果在 keyup 事件之后計時器在 3 秒 (doneTypingInterval) 后沒有被重置,元素將被發送到一個自動保存函數,該函數通過 ajax 發布元素名稱和值。我遇到的問題是,如果我輸入一個輸入(即:#input-name),一秒鐘后我輸入另一個(即:#input-shortName),第一個輸入(#input-name)是從未發送到自動保存功能。有沒有一種好的方法可以在不為每個輸入創建唯一的 typingTimer 和 on 事件的情況下做到這一點?我以這種方式嘗試過它并且它有效,但我確信必須有更好的方法。let typingTimer;  const doneTypingInterval = 3000;  const $input = $('#input-name, #input-shortName, #input-email');  $input    .on('keyup paste', function () {      if($.fn.isAutoSaveOn()) {        clearTimeout(typingTimer);        typingTimer = setTimeout($.fn.autoSaving, doneTypingInterval, this);      }    })    .on('keydown', function () {      if($.fn.isAutoSaveOn()) {        clearTimeout(typingTimer);      }    })  ;$.fn.autoSaving = function(e) {    const autosave = $('#autosave')    if(autosave.hasClass('active')) {      autosave.html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>&nbsp;' + lang_saving);      const element = $(e);      const url = '/v3/api/orgs/' + orgId + '/update';      const input = JSON.stringify({        field: element.attr('name'),        value: element.val()      });      $.ajax({        type: "POST",        url: url,        data: input,        contentType: "application/json"      })      .done(function(response) {        notify("Success", response, "bottom", "right", "ni ni-bell-55", "success", "animated fadeIn", "animated fadeOut");      })      .fail(function(response) {          notify("Error", response, "bottom", "right", "ni ni-bell-55", "error", "animated fadeIn", "animated fadeOut");      })      .always(function() {        $.fn.autoSaveOn();      });    }  }
查看完整描述

2 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

您可以為輸入提供一個自動保存時間戳,然后檢查它是否已經過去,而不是試圖讓超時彼此重疊。


let auto_save_timer = null;

    

$('input.auto-save').on('keyup paste', function() {

  this.setAttribute('data-auto-save-timeout', (new Date()).getTime() + 3000); //set save time


  if (!auto_save_timer) {

    auto_save_timer = setInterval(function() {

      let $inputs = $('input[data-auto-save-timeout]');


      if ($inputs.length) {

         $inputs.each(function() {

           if ((new Date()).getTime() - this.attributes['data-auto-save-timeout'].value >= 0) {

             this.removeAttribute('data-auto-save-timeout');

             your_save_function(this);

          }

         });

       } else {

         clearInterval(auto_save_timer); //stops the timer

         auto_save_timer = null; //for checking if the timer is active,

                                 //clearInterval() doesn't make it false      

                                 //this prevents multiple timers from overlapping

      }

    }, 1000);

  }

});

https://jsfiddle.net/sjmdqhgu/


查看完整回答
反對 回復 2023-03-24
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

您可以為輸入提供一個自動保存時間戳,然后檢查它是否已經過去,而不是試圖讓超時彼此重疊。


let auto_save_timer = null;

    

$('input.auto-save').on('keyup paste', function() {

  this.setAttribute('data-auto-save-timeout', (new Date()).getTime() + 3000); //set save time


  if (!auto_save_timer) {

    auto_save_timer = setInterval(function() {

      let $inputs = $('input[data-auto-save-timeout]');


      if ($inputs.length) {

         $inputs.each(function() {

           if ((new Date()).getTime() - this.attributes['data-auto-save-timeout'].value >= 0) {

             this.removeAttribute('data-auto-save-timeout');

             your_save_function(this);

          }

         });

       } else {

         clearInterval(auto_save_timer); //stops the timer

         auto_save_timer = null; //for checking if the timer is active,

                                 //clearInterval() doesn't make it false      

                                 //this prevents multiple timers from overlapping

      }

    }, 1000);

  }

});

https://jsfiddle.net/sjmdqhgu/


查看完整回答
反對 回復 2023-03-24
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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