Is there any way to speed up this solution for a case-insensitive jQuery :contains selector?

前端 未结 5 1083
萌比男神i
萌比男神i 2021-02-09 14:11

I found this solution for a case-insensitive jQuery :contains selector on StackOverflow. It works great, however it comes at the cost of performance. Does anyone el

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-09 14:23

    You could try to check the selector only once, after the user has stopped typing for a specified amount of time, not for every keystroke.

    For example, a simple implementation:

    Usage:

    $("#textboxId").keyup(function () {
      typewatch(function () {
        // executed only 500 ms after the user stopped typing.
      }, 500);
    

    Implementation:

    var typewatch = function(){
        var timer = 0;  // store the timer id
        return function(callback, ms){
            clearTimeout (timer);  // if the function is called before the timeout
            timer = setTimeout(callback, ms); // clear the timer and start it over
        }  
    }();
    

提交回复
热议问题