63、商品搜索_防抖功能
防抖(防止抖动)定时器
防抖一般输入框中防止重复输入重复发送请求(定义全局的定时器id)
节流一般是用在页面下拉和上拉
pages\search\index.js
// 0 引入 来发送请求的方法 一定要把路径补全
import {request} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
/**
* 页面的初始数据
*/
data: {
goods:[]
},
TimeId:-1,
// 输入框的值改变就会触发的事件
handleInput(e){
// 1 获取输入框的值
const {value}=e.detail;
// 2 检测合法性
if(!value.trim()){
// 值不合法
return;
}
// 3 准备发送请求获取数据
clearTimeout(this.TimeId);
this.TimeId=setTimeout(()=>{
this.qsearch(value);
}, 1000);
},
// 发送请求获取搜索建议数据
async qsearch(query){
const res=await request({url: "/goods/qsearch", data:{query}});
console.log(res);
this.setData({
goods:res
})
}
})