前端项目javascript笔记(二)

12-21更新

  1. 获取当前时间
function getTime() {
    var date = new Date();
    var Year = date.getFullYear(),
        Month = date.getMonth() + 1,
        Day = date.getDate(),
        Hours = date.getHours(),
        Minutes = date.getMinutes(),
        Seconds = date.getSeconds();
    Month = Month < 10 ? '0' + Month : Month;
    Day = Day < 10 ? '0' + Day : Day;
    Hours = Hours < 10 ? '0' + Hours : Hours;
    Minutes = Minutes < 10 ? '0' + Minutes : Minutes;
    Seconds = Seconds < 10 ? '0' + Seconds : Seconds;
    var day = Year + '-' + Month + '-' + Day + ' ' + Hours + ':' + Minutes + ':' + Seconds;
    return day;
}

  1. 处理数据强制保留两位小数(四舍五入)
function toDecimal(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}
//测试
var a='22';
var b=toDecimal(a)   //22.00

  1. 获取验证码倒计时效果
function countDown() {
    var count = 60;
    var timer = setInterval(function () {
        count--;
        $('.getting').text('重新获取' + count + 's');
        if (count < 1) {
            clearInterval(timer);
            timer = null;
            $('.get').removeClass('getting').text('获取验证码');
            count = 60;
        }
    }, 1000);
}

猜你喜欢

转载自blog.csdn.net/qq_41194534/article/details/85157740