Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript auto save input

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
  });
  
});
 
PREVIOUS NEXT
Tagged: #javascript #auto #save #input
ADD COMMENT
Topic
Name
8+1 =