React 实现搜索防抖

import React from "react";

export default function debounce<T = HTMLInputElement>(
  fn: (ev: React.ChangeEvent<T>) => void,
  ms = 500
) {
  let timeId: NodeJS.Timeout;

  return (ev: React.ChangeEvent<T>) => {
    clearTimeout(timeId);

    timeId = setTimeout(() => {
      fn(ev);
    }, ms);
  };
}

posted @ 2022-05-11 17:46  Rustln  阅读(114)  评论(0编辑  收藏  举报