21、防抖高级
<body>
<div id="ni" class="hao"></div>
<script>
function debounce1(func, wait) {
let timeout;
debugger
return function () {
let context = this;
let args = arguments;
clearTimeout(timeout)
timeout = setTimeout(function () {
func.apply(context, args)
}, wait);
}
}
function debounce2(func, wait, immediate) {
let timeout, result;
let debounced = function () {
var context = this;
var args = arguments;
clearTimeout(timeout);
if (immediate) {
let callNow = !timeout;
timeout = setTimeout(function () {
timeout = null;
}, wait)
if (callNow) result = func.apply(context, args)
} else {
timeout = setTimeout(function () {
result = func.apply(context, args)
}, wait);
}
return result;
};
debounced.cancel = function () {
clearTimeout(timeout);
timeout = null;
};
return debounced;
}
let count = 1;
let myDiv = document.getElementById('ni')
function getUserAction(e) {
console.log(this, e)
myDiv.innerHTML = count++;
};
myDiv.onclick = debounce1(getUserAction, 1000)
</script>
</body>
22、节流高级
<body>
<div id="ni" class="hao"></div>
<script>
function throttle1(func, wait) {
let context, args;
let previous = 0;
return function () {
let now = new Date();
context = this;
args = arguments;
debugger
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
}
}
function throttle(func, wait, options) {
let timeout, context, args, result;
let previous = 0;
if (!options) options = {};
let later = function () {
previous = options.leading === false ? 0 : new Date().getTime();
timeout = null;
func.apply(context, args);
if (!timeout) context = args = null;
};
var throttled = function () {
var now = new Date().getTime();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
};
throttled.cancel = function () {
clearTimeout(timeout);
previous = 0;
timeout = null;
}
return throttled;
}
let count = 1;
let myDiv = document.getElementById('ni')
debugger
function getUserAction(e) {
console.log(this, e)
myDiv.innerHTML = count++;
};
myDiv.onclick = throttle1(getUserAction, 1000)
</script>
</body>
23、函数柯里化
<body>
<script>
function curry(fn) {
let judge = (...args) => {
if (args.length == fn.length) {
return fn(...args)
} else {
return (...arg) => judge(...args, ...arg)
}
}
return judge;
}
function add(a, b, c) {
return a + b + c
}
let addCurry = curry(add)
debugger
addCurry(1)(2)(3)
function log(date, importance, message) {
alert(`[${date.getHours()}:${date.getMinutes()}] [${importance}] ${message}`);
}
let logCurry = curry(log);
logCurry(new Date())('DEBUG')('There is some bugs!');
let logNow = logCurry(new Date());
logNow('INFO','Here is bug!');
let debugNow = logNow('DEBUG');
debugNow('There is some bugs!!!');
</script>
</body>
24、偏函数
<script>
function partial(fn, ...args) {
return (...arg) => {
return fn(...args, ...arg)
}
}
function add(a, b, c) {
return a + b + c
}
debugger
let partialAdd = partial(add, 1)
partialAdd(2, 3)
</script>
25、JSONP
<body>
<script>
const jsonp = ({ url, params, callbackName }) => {
const generateUrl = () => {
let dataSrc = ''
for (let key in params) {
if (params.hasOwnProperty(key)) {
dataSrc += `${key}=${params[key]}&`
}
}
dataSrc += `callback=${callbackName}`
return `${url}?${dataSrc}`
}
return new Promise((resolve, reject) => {
const scriptEle = document.createElement('script')
scriptEle.src = generateUrl()
document.body.appendChild(scriptEle)
window[callbackName] = data => {
resolve(data)
document.removeChild(scriptEle)
}
})
}
</script>
</body>