js
所谓防抖,就是指触发事件后在 n 秒内函数只能执行一次,如果在 n 秒内又触发了事件,则会重新计算函数执行时间。
防抖核心函数
function debounce(fn, delay) {
let timeout = null;
return function() {
// 保存this和参数
let context = this
let args = arguments
if(timeout) {
clearTimeout(timeout) //频繁触发则清除上一次,只执行最后一次
}
timeout = setTimeout(() => {
fn.apply(context, args)
},delay)
}
}
所谓节流,就是指连续触发事件但是在 n 秒中只执行一次函数。节流会稀释函数的执行频率。
节流核心函数
function throttle(fn, timer) {
let start = new Date()
let timeout = null
return function() {
let end = new Date
let context = this
clearTimeout(timeout)
if (end - start >= timer) {
fn.apply(context)
start = end
}
}
}
get 请求传参长度的误区
- HTTP 协议 未规定 GET 和 POST 的长度限制
- GET 的最大长度显示是因为 浏览器和 web 服务器限制了 URI 的长度
- 不同的浏览器和 WEB 服务器,限制的最大长度不一样
- 要支持 IE,则最大长度为 2083byte,若只支持 Chrome,则最大长度 8182byte
网友评论