How to apply a timeout if a function hasn't been called 1 of N records in 3 seconds

后端 未结 2 1579
Happy的楠姐
Happy的楠姐 2021-01-26 12:39

When a user performs a certain action (typing) I run a function that applies a specific styling to that user in a list of 100s of users. As the user continues to type this funct

2条回答
  •  一个人的身影
    2021-01-26 13:29

    Use JavaScript's setTimeout() method to invoke a method to remove the styling after 3 seconds (3000 ms). See below:

    var timeout;
    
    function clearStyling() {
        // clear your styling here
    };
    
    $("#typingBox").on("keypress", function() {
        clearTimeout(timeout);
        timeout = setTimeout(clearStyling, 3000);
        // do your styling here ...
    });
    

提交回复
热议问题