//驼峰命名法
var str = ‘doucment-get-element-byid’
var arr = str.split(’-’);
for(var i = 0;i < arr.length; i++) {
//吧arr[i]中的第0项变为大写 如何加上arr[i] 减去一项
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1,arr[i].length-1)
}
console.log(arr)
//函数节流
function handleAdd(fn, t) {
var lastTime = 0; //记录上一次函数触发的时间
return function () {
var nowTime = Date.now(); //记录当前时间
if (nowTime - lastTime > t) {
fn.apply(this)
lastTime = nowTime //同步时间
}
}
}
document.getElementById(‘btn’).onclick = handleAdd(function () { console.log(‘我被点击了1’) }, 2000)
//函数防抖
function handleAdd(fn, t) {
var timer = null;
return function () {
clearTimeout(timer)
timer = setTimeout(function () {
fn.apply(this)
}, t)
}
}
document.getElementById(‘btn’).onclick = handleAdd(function () { console.log(‘我被点击了2’) }, 2000)