小程序中实现节流搜索

392 阅读1分钟

最近公司安排了一项节流搜索的开发任务给我,在这将节流搜索的主要代码记录在此,这里主要是通过clearTimeout 和 setTimeout搭配会用实现了节流搜索

<script>
const delay = (function() {
  let timer = 0;
  return function(callback, ms) {
    clearTimeout(timer);
    timer = setTimeout(callback, ms);
  };
})();
 export default class Search extends wepy.page {
   
   methods: {
    /**
     * 搜索,节流操作处理
     */
    changeGoodsTitle(e) {
      this.goodsTitle = e.detail;
      delay(() => {
        this.searchProduct();
      }, 3000);
    },
   }
 }
</style>