// packageD/mycomponent/agency-saleman/agency-saleman.js Component({ /** * 组件的属性列表 */ properties: { mid: { type: [String, Number], value: '0' } }, /** * 组件的初始数据 */ data: { error: false, timeoutId: null, //防抖,防止触发 self_member_id: "", placeholder: "请输入销售员ID(选填)" }, /** * 组件的方法列表 */ methods: { inputChange(e) { if (this.data.error) { this.setData({ error: false }); } if (e.detail == this.data.mid) { this.setData({ placeholder: '禁止输入自己的销售员ID' }); } this.setData({ self_member_id: e.detail }); if (this.data.timeoutId) { clearTimeout(this.data.timeoutId); // 先清除之前的延时,并在下面重新开始计算时间 } // clearTimeout方法,以便控制连续触发带来的无用调用 this.data.timeoutId = setTimeout(() => { // time 毫秒以后执行方法 this.blurInput(); }, 1500); // 如果还没有执行就又被触发,会根据上面的clearTimeout来清除并重新开始计算 }, blurInput() { if (this.data.self_member_id == this.data.mid) { this.setData({ error: true, self_member_id: '' }); } } } });