第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 keyup/keydown 事件上具有唯一超時(shí)的 jquery 多個(gè)選擇器

在 keyup/keydown 事件上具有唯一超時(shí)的 jquery 多個(gè)選擇器

千萬(wàn)里不及你 2023-03-24 17:17:22
我正在嘗試創(chuàng)建一個(gè)自動(dòng)保存輸入到幾個(gè)文本框中的輸入的功能。我在一個(gè)選擇器 ($input) 中有 3 個(gè)輸入,還有一個(gè)在 keyup/paste 和 keydown 上更新的計(jì)時(shí)器 (typingTimer)。如果在 keyup 事件之后計(jì)時(shí)器在 3 秒 (doneTypingInterval) 后沒(méi)有被重置,元素將被發(fā)送到一個(gè)自動(dòng)保存函數(shù),該函數(shù)通過(guò) ajax 發(fā)布元素名稱和值。我遇到的問(wèn)題是,如果我輸入一個(gè)輸入(即:#input-name),一秒鐘后我輸入另一個(gè)(即:#input-shortName),第一個(gè)輸入(#input-name)是從未發(fā)送到自動(dòng)保存功能。有沒(méi)有一種好的方法可以在不為每個(gè)輸入創(chuàng)建唯一的 typingTimer 和 on 事件的情況下做到這一點(diǎn)?我以這種方式嘗試過(guò)它并且它有效,但我確信必須有更好的方法。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 回答

?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以為輸入提供一個(gè)自動(dòng)保存時(shí)間戳,然后檢查它是否已經(jīng)過(guò)去,而不是試圖讓超時(shí)彼此重疊。


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/


查看完整回答
反對(duì) 回復(fù) 2023-03-24
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

您可以為輸入提供一個(gè)自動(dòng)保存時(shí)間戳,然后檢查它是否已經(jīng)過(guò)去,而不是試圖讓超時(shí)彼此重疊。


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/


查看完整回答
反對(duì) 回復(fù) 2023-03-24
  • 2 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)