小练习

120 阅读1分钟
 <div id="content" style="height:150px;line-height:150px;text-align:center; color: #fff;background-color:#ccc;font-size:80px;"></div>
    <script>
        //一
// function setCookie(key,val,expires){
//     let now = new Date()
//     now.setHours(now.getHours()+expires)
//     document.cookie = `${key}=${val};expires=${now.toUTCString()}`
// }
// setCookie("nickname","Ace",24*7)
// console.log(document.cookie)
// function removeCookie(key){
//     this.setCookie(key,null,-1)
// }
//二
// setTimeout(()=>{
//     removeCookie("nickname")
//     console.log(document.cookie)
// },1000)
//获取值
// let local = 'age=20&price=30&keywords=lv&cc=1'

//     let obj={}
// local.split("&").forEach(i=>{
//     let kev = i.split("=")
// obj[kev[0]]=kev[1]
// })
// console.log(obj.keywords)
//获取大小值
// function num(max,min){
//     return a=Math.floor(Math.random()*(max-min))+min
// }
// console.log(num(40,30))
//获取当前星期
// let now = new Date()
// let arr = ["日","一","二","三","四","五","六"]
// console.log("星期"+arr[now.getDay()])
// //用递归的方式获取10以内数字的和
// function sum(num){
// if (num==1) return 1;
// return num+sum(num-1)
// }
// console.log(sum(10))
// //手写一个补0,如repairZero(8)变成08
// function repairZero(num){
// if (num<10) return "0"+num;
// return num
// }
// console.log(repairZero(8))

//防抖debounce

//     let num = 1;
//     let content = document.getElementById('content');

//     function count() {
//         content.innerHTML = num++;
//     };
//     function debounce(func, wait) {
//     let timeout;
//     return function () {

//         if (timeout) clearTimeout(timeout);
        
//         timeout = setTimeout(() => {
//             func.apply(this, arguments)
//         }, wait);
//     }
// }

// content.onmousemove = debounce(count,1000);
//立即执行
/* 
function debounce(func,wait) {
    let timeout;
    return function () {


        if (timeout) clearTimeout(timeout);

        let callNow = !timeout;
        timeout = setTimeout(() => {
            timeout = null;
        }, wait)

        if (callNow) func.apply(this, arguments)
    }
}

*/


//节流函数:
//时间戳
    let num = 1;
    let content = document.getElementById('content');

    function count() {
        content.innerHTML = num++;
    };
function throttle(fn, wait){
let endtime = new Date();
return function(){
    let that = this
if (new Date()-endtime>wait) {
    fn.apply(that,arguments)
    endtime = new Date()
}
}
}
content.onmousemove = throttle(count,1000);
    </script>