达梦数据charindex_吐血整理!!!2019最强前端面试合集(编程题)

更新时间:2023-07-17 14:16:12 阅读: 评论:0

达梦数据charindex_吐⾎整理2019最强前端⾯试合集
(编程题)
编程题
⽅法库部分
实现防抖函数(debounce)
const debounce = (fn, delay) => {
let timer = null;
京的成语
return (...args) => {
clearTimeout(timer);9999打一成语
timer = tTimeout(() => {
fn.apply(this, args);
夏洛的网推荐理由
}, delay);
};
};
underscore版本:
_.debounce = function(func, wait, immediate) {
var timeout, result;
var later = function(context, args) {
timeout = null;
if (args) result = func.apply(context, args);
};
var debounced = restArguments(function(args) {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = tTimeout(later, wait);
if (callNow) result = func.apply(this, args);
} el {
timeout = _.delay(later, wait, this, args);
}
return result;
});
debounced.cancel = function() {
clearTimeout(timeout);
timeout = null;
};
return debounced;
};
lodash版本
import isObject from './isObject.js'
import root from './.internal/root.js'
function debounce(func, wait, options) {
let lastArgs,
lastThis,
maxWait,
result,
timerId,
八公的故事lastCallTime
let lastInvokeTime = 0
let leading = fal
let maxing = fal
let trailing = true
const uRAF = (!wait && wait !== 0 && questAnimationFrame === 'function') if (typeof func !== 'function') {
throw new TypeError('Expected a function')
}
wait = +wait || 0
if (isObject(options)) {
leading = !!options.leading
maxing = 'maxWait' in options
maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : maxWait
trailing = 'trailing' in options ? !!ailing : trailing
}
function invokeFunc(time) {
const args = lastArgs
const thisArg = lastThis
lastArgs = lastThis = undefined
lastInvokeTime = time毛泽东传记
result = func.apply(thisArg, args)
return result
}
function startTimer(pendingFunc, wait) {
if (uRAF) {
root.cancelAnimationFrame(timerId)
questAnimationFrame(pendingFunc)
}
return tTimeout(pendingFunc, wait)
}
function cancelTimer(id) {
if (uRAF) {
return root.cancelAnimationFrame(id)
}
clearTimeout(id)
}
function leadingEdge(time) {
深圳地产公司排名lastInvokeTime = time
timerId = startTimer(timerExpired, wait)
return leading ? invokeFunc(time) : result
}
function remainingWait(time) {
const timeSinceLastCall = time - lastCallTime
const timeSinceLastInvoke = time - lastInvokeTime
const timeWaiting = wait - timeSinceLastCall
return maxing
Math.min(timeWaiting, maxWait - timeSinceLastInvoke)
: timeWaiting
}
function shouldInvoke(time) {
const timeSinceLastCall = time - lastCallTime
const timeSinceLastInvoke = time - lastInvokeTime
return (lastCallTime === undefined || (timeSinceLastCall >= wait) ||
(timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)) }
function timerExpired() {
中秋节周记const time = w()
if (shouldInvoke(time)) {
return trailingEdge(time)
}
timerId = startTimer(timerExpired, remainingWait(time))
}
function trailingEdge(time) {
timerId = undefined
if (trailing && lastArgs) {
return invokeFunc(time)
}
lastArgs = lastThis = undefined
return result
}
function cancel() {
if (timerId !== undefined) {
cancelTimer(timerId)
}
lastInvokeTime = 0
专题组织生活会
lastArgs = lastCallTime = lastThis = timerId = undefined
}
function flush() {
return timerId === undefined ? result : w())
}
function pending() {
return timerId !== undefined
}
function debounced(...args) {
const time = w()
const isInvoking = shouldInvoke(time)
lastArgs = args
lastThis = this
lastCallTime = time
if (isInvoking) {
if (timerId === undefined) {
return leadingEdge(lastCallTime)
}
if (maxing) {
timerId = startTimer(timerExpired, wait) return invokeFunc(lastCallTime)
}
}
if (timerId === undefined) {
timerId = startTimer(timerExpired, wait) }
return result
}
debounced.cancel = cancel debounced.flush = flush
debounced.pending = pending
return debounced
}
export default debounce
实现节流函数(throttle)
const throttle = (fn, delay = 500) => { let flag = true;
return (...args) => {
if (!flag) return;
flag = fal;
tTimeout(() => {
fn.apply(this, args);
flag = true;
}, delay);
};
};

本文发布于:2023-07-17 14:16:12,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1085157.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实现   函数   深圳   部分
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图