📌  相关文章
📜  javascript 自动保存输入 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:15.882000             🧑  作者: Mango

代码示例1
var saveTimeoutId;

document.addEventListener("DOMContentLoaded", () => {
  // Get the element(s) you want to autosave
  let area = document.getElementById('area');
  
  // use keyup over keypress so that backspaces will register
  area.addEventListener('keyup', () =>{
    
    // clear save timeout as the user is editing
    if (saveTimeoutId) window.clearTimeout(saveTimeoutId);
    
    // Store the timeout id again
    saveTimeoutId = window.setTimeout(() =>{
      ...
      // Code for saving here
      ...
    }, 1000); // Configure timeout period to preference
  });
  
});