# 防抖、节流

const throttle = function (func, delay) {
  var timer = null;
  return function () {
    var context = this;
    var args = arguments;
    if (!timer) {
      timer = setTimeout(function () {
        func.apply(context, args);
        timer = null;
      }, delay);
    }
  }
}

function handle() {
  console.log(Math.random());
}

window.addEventListener('scroll', throttle(handle, 1000));

function debounce(fn, wait) {
    // 使用闭包保存持久变量
    var timeout = null;
    return function() {
        if(timeout !== null) 
                // 如果持续触发,就把前面的定时器取消掉,这样来保证只有不触发事件时才开始计时
                clearTimeout(timeout);
        timeout = setTimeout(fn, wait);
    }
}
// 处理函数
function handle() {
    console.log(Math.random()); 
}
// 滚动事件
window.addEventListener('scroll', debounce(handle, 1000));

# jsonp

function msg(content){
  alert(content)
}

function jsonp(req){
    var script = document.createElement('script');
    var url = req.url + '?callback=' + req.callbackName;
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script); 
}

jsonp({url:'http://www.domain.com/say', callbackName:msg})

// server.js
let express = require('express')
let app = express()
app.get('/say', function(req, res) {
  let { callback } = req.query
  // 返回的内容是脚本的内容
  res.end(`${callback}('I am server data')`)
})
app.listen(3000)

# axios

import loading from ""
import error from ""
let http=axios.create({
    baseURL:'http://localhost:8080',//基本地址
    timeout:1000,//超时时长
})
http.interceptors.request.use(config=>{
    config.headers.token=''
    loading.show()
    return config
},err=>{
    error.show()//提示框提示请求错误
    setTime(()=>{
        error.hide()
    },2000)
    return Promise.reject(err)
})

http.interceptors.response.use(res=>{
    //操作
    loading.hide()
    return res
},err=>{
    loading.hide()
    error.show()
    setTime(()=>{
        error.hide()
    },2000)
    return Promise.reject(err)
})

export default http