JS(二十一)有用的JS函数(持续更新)
来源:互联网 发布:sql语句计算平均值 编辑:程序博客网 时间:2024/06/05 19:12
1、数字转化为千分位分割符格式
window.convertThousands = function (num) { var prefix, suffix, sign; if (isNaN(Number(num))) { return num; } sign = (num >= 0) ? '' : '-'; num = (Math.abs(num) || 0).toString(); if (num.indexOf('.') >= 0) { prefix = num.split('.')[0]; suffix = num.split('.')[1]; } else { prefix = num; } var result = ''; while (prefix.length > 3) { result = ',' + prefix.slice(-3) + result; prefix = prefix.slice(0, prefix.length - 3); } if (prefix) { result = sign + prefix + result; } if (suffix) { result = result + '.' + suffix; } return result; };
2、函数节流和去抖
window.debounce = function (func, wait, immediate) { var timeout; return function () { var context = this, args = arguments; var later = function () { timeout = null; if (!immediate) { func.apply(context, args); } }; if (immediate && !timeout) { func.apply(context, args); } clearTimeout(timeout); timeout = setTimeout(later, wait); }; }; /* dependent on debounce */ window.throttle = function (func, wait) { var context, args, timeout, throttling, more, result; var whenDone = debounce( function () { more = throttling = false; }, wait); return function () { context = this; args = arguments; var later = function () { timeout = null; if (more) { func.apply(context, args); } whenDone(); }; if (!timeout) { timeout = setTimeout(later, wait); } if (throttling) { more = true; } else { result = func.apply(context, args); } whenDone(); throttling = true; return result; }; };3、计算两个日期相差的日期年月日等
/* 计算两日期相差的日期年月日等 */ Date.prototype.dateDiff = function (objDate2, interval) { var d = this, i = {}, t = d.getTime(), t2 = objDate2.getTime(); i['y'] = objDate2.getFullYear() - d.getFullYear(); i['q'] = i['y'] * 4 + Math.floor(objDate2.getMonth() / 4) - Math.floor(d.getMonth() / 4); i['m'] = i['y'] * 12 + objDate2.getMonth() - d.getMonth(); i['ms'] = objDate2.getTime() - d.getTime(); i['w'] = Math.floor((t2 + 345600000) / (604800000)) - Math.floor((t + 345600000) / (604800000)); i['d'] = Math.floor(t2 / 86400000) - Math.floor(t / 86400000); i['h'] = Math.floor(t2 / 3600000) - Math.floor(t / 3600000); i['n'] = Math.floor(t2 / 60000) - Math.floor(t / 60000); i['s'] = Math.floor(t2 / 1000) - Math.floor(t / 1000); return i[interval]; };
-----------------------------------------------------------------------------------------------------------------------------------------------------------
2017.12.15 更新
4、获取url中的参数
/* 获取URL查询参数 */ window.getQueryString = function (param) { var sQuery = window.location.search; if (sQuery && sQuery.length > 0) { var arr = sQuery.substr(1).split('&'); for (var i in arr) { var pair = arr[i].split('='); if (param && param.toLowerCase() === pair[0].toLowerCase() && pair[1]) { return pair[1]; } } } return ''; };5、封装jquery的ajax方法
var getDataSource = function (api, dataparam, successFunc, errorFunc) { $.ajax({ url: api, type: 'GET', data: dataParam dataType: 'json', contentType: 'application/json', success: function (response) { successFunc.call(this, response); }, error: function () { errorFunc.call(this); } });}
阅读全文
0 0
- JS(二十一)有用的JS函数(持续更新)
- 有用的函数集(持续更新)
- 有用的JS(二)
- 有用的JS效果网址收集(持续更新中,欢迎评论并补充)
- python 一些有用的功能函数(持续更新)
- js积累(持续更新)
- 前端js优化方案(二)持续更新
- 常用js函数收集(持续更新中----)
- 总结js常用函数和常用技巧(持续更新)
- 总结js常用函数和常用技巧(持续更新)
- 总结js常用函数和常用技巧(持续更新)
- 一些有用的链接(持续更新)
- 有用的adb 命令(持续更新)
- js 数组的内置方法(持续更新)
- JS 知识点积累(持续更新)
- JS常见问题集合(持续更新)
- js大全(持续更新中)
- JS 自定义代码库(持续更新)
- 我的第一个编程
- X-Frame-Options 响应头配置避免点击劫持攻击
- QT与JaveScript 交互
- python中urllib.quote出现KeyError
- git修改name和email
- JS(二十一)有用的JS函数(持续更新)
- Dubbo的超时重试机制带来的数据重复问题
- js插件--1.swal
- vue中的路由编写方法
- 一个特殊的FTP错误550
- 一个战五渣的cookie使用过程
- docker部署spring cloud项目
- [LeetCode]303. Range Sum Query
- Percona Server 5.7 并行doublewrite 特性