你要的Js工具函数【第二期】

2022-07-01 16:11:57 浏览数 (1)

防抖

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <input type="submit" value="send" onclick="send()">
</body>
<script>
  function send() {
    debounce(() => {
          console.log('1')
      }, 2000)()
  }
  const debounce = (fn, delay) => {
      let timer = null;
      return () => {
          clearTimeout(timer);
          timer = setTimeout(() => {
              fn.apply(this)
          }, delay)
      }
  }
</script>
</html>
123456789101112131415161718192021222324252627

时间转化

代码语言:javascript复制
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

</body>
<script type="text/javascript">
    // 将秒化为时分秒
    formateSeconds(endTime) {
      let secondTime = parseInt(endTime); //将传入的秒的值转化为Number
      let min = 0; // 初始化分
      let h = 0; // 初始化小时
      let result = "";
      if (secondTime > 60) {
        //如果秒数大于60,将秒数转换成整数
        min = parseInt(secondTime / 60); //获取分钟,除以60取整数,得到整数分钟
        secondTime = parseInt(secondTime % 60); //获取秒数,秒数取佘,得到整数秒数
        if (min > 60) {
          //如果分钟大于60,将分钟转换成小时
          h = parseInt(min / 60); //获取小时,获取分钟除以60,得到整数小时
          min = parseInt(min % 60); //获取小时后取佘的分,获取分钟除以60取佘的分
        }
      }
      result = `${h.toString().padStart(2, "0")}:${min.toString().padStart(2, "0")}:${secondTime.toString().padStart(2, "0")}`;
      return result;
    },
    // 将时分秒化为秒
1234567891011121314151617181920212223242526272829

更多内容请见原文,原文转载自:http://www.mark-to-win.com/tutorial/50903.html

0 人点赞