Skip to content

一些工作中写过的可能有用的代码片段 - 持续更新

Notifications You must be signed in to change notification settings

TerryBeanX2/myUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 

Repository files navigation

一些工作中写过的可能有用的代码片段

  • 写了个格式化银行卡的方法
function formatBankcard(str){
    return str.split('').reduce((prev, next, index)=> {
        return prev + ((index % 4) ? next : (' ' + next ));
    });
}

let str = '6214143523234154320';
formatBankcard(str) //6214 1435 2323 4154 320
  • 格式化货币的正则(借鉴来的)
var test1 = '1234567890'
var format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
console.log(format) // 1,234,567,890

//或者

(23333333).toLocaleString('en-US')  // 23,333,333
  • 节流函数
function throttle(fn, context) {
    clearTimeout(fn.timeoutId);
    fn.timeoutId = setTimeout(fn.bind(context), 500);
}
  • input中文键盘处理
    input.addEventListener('compositionstart', fn);
    input.addEventListener('compositionend', fn);
  • 列表停止滑动、触底
const body = document.body;
const html = document.documentElement;
body.onscroll = () => {
    throttle(fnCheckListStop);
    if (html.scrollHeight - html.clientHeight > 0) {
      ((html.scrollHeight - html.clientHeight) <= (html.scrollTop || body.scrollTop)) && fnCheckListTouchBottom;
    }
}

About

一些工作中写过的可能有用的代码片段 - 持续更新

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published