防抖和节流
1、第一个案例
debounce(dispatch({ type: 'subTask/getSubTaskUrls', payload: { name: v } }), 1000);
function debounce(fn, wait) { var timeout = null; return function() { if(timeout !== null) clearTimeout(timeout); timeout = setTimeout(fn, wait); } }
2、第二个案例
export function fusionDebounce(action, idle) { let timer; return function() { // tslint:disable-next-line: no-this-assignment const context = this; const args = arguments; return new Promise((resolve, reject) => { clearTimeout(timer); timer = setTimeout(() => { action.apply(context, args) .then(r => { resolve(r); }) .catch(err => { reject(err); }); }, idle); }); };D }
<FormItem label='子任务名称:' required hasFeedback requiredMessage='请输入子任务名称' validator={fusionDebounce(this.titleExists, 1000)} >
titleExists = (rules, value) => { const { subTask: { tmpName } } = this.props; const { location: { query: { editType = '0' } } } = this.props.history; return new Promise((resolve, reject) => { if (value.length > 20) { reject([new Error('任务名称不能大于20')]); } if (editType === '1' && value === tmpName) { resolve(); return; } checkDupName(value).then(r => { if (r.result === true) { reject('该子任务名称已被使用'); return; } resolve(); }).catch(err => { Message.error('网络出错'); }); }); };
漫思