Untitled

 avatar
unknown
javascript
a year ago
1.1 kB
10
Indexable
const debounce = (func, delay) => {
    let timeoutId;
    return (...args) => {
        clearTimeout(timeoutId);
        timeoutId = setTimeout(() => {
            func.apply(null, args);
        }, delay);
    };
};

// Function to set up input field backup and restore
function setupInputBackup(inputSelector) {
    const inputField = document.querySelector(inputSelector); // Select the input element
    if (inputField) {
        inputField.addEventListener('input', debounce(() => localStorage.setItem('inputBackup', inputField.value), 300)); // Backup on input with debounce
        inputField.value = localStorage.getItem('inputBackup') || ''; // Restore the input value

        // Clear local storage when Enter key is pressed
        inputField.addEventListener('keydown', (event) => {
            if (event.key === 'Enter') { // Check if the pressed key is Enter
                localStorage.removeItem('inputBackup'); // Clear the backup from localStorage
            }
        });
    }
}

// Call the function with the selector for the input field
setupInputBackup('.text');
Editor is loading...
Leave a Comment