js 计算剩余 天、时、分、秒



/**  * 计算剩余 天、时、分、秒  * @param datetime long 型  * @param d  * @returns {*}  * @constructor  */ function CountDown(datetime, d){
    var now = new Date();
    var leftTime = new Number(datetime) + 3600000*24*d - now.getTime();
    if( leftTime > 0 ) {
        var leftsecond = parseInt(leftTime/1000);
        var day = Math.floor(leftsecond/(60*60*24));
        var hour = Math.floor((leftsecond-day*24*60*60)/3600);
        var minute = Math.floor((leftsecond-day*24*60*60-hour*3600)/60);
        return {
            leftTime: leftTime,
            leftsecond: leftsecond,
            day: day,
            hour: hour,
            minute: minute,
            second: Math.floor(leftsecond-day*24*60*60-hour*3600-minute*60)
        };
    } else {
        return {
            leftTime : leftTime
        };
    }
}

/**  * 倒计时 - 显示倒计时  * @param data complaint json  * @constructor  */ function ShowCountDownForUndo(data) {
    var left = CountDown(data.longgeneratetime, 4);//4 
    if( left.leftTime > 0 ) {
        $("#warnmsg").html( ( left.day*24 + left.hour) + '小时' + left.minute + '  ' + left.second + ' ');
    } else {

        clearInterval(timer);
    }
}



/**  * 计算剩余可输入字符,英文占半个字符  * @param f textarea  * @param maxstrlen  * @param leftCharsEl element id 显示提示信息的dom节点  */ function calcLeftCharactors(f, maxstrlen, leftCharsDiv) {
    var str = f.value;
    var myLen = 0, i = 0;

    for (; (i < str.length) && (myLen <= maxstrlen * 2); i++) {
        if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 128)
            myLen++;
        else  myLen += 2;
    }
    if (myLen > maxstrlen * 2) {
        f.value = str.substr(0, i-1);
        $('#'+leftCharsDiv).html( 0 );
    } else {
        $('#'+leftCharsDiv).html( Math.min(maxstrlen, Math.floor((maxstrlen * 2 - myLen) / 2)) );
    }
}

//计算剩余可输入字数
$('#content_').on('keyup mousedown blur paste', function(){
    var me = this;
    setTimeout(function(){
        calcLeftCharactors(me, 100, 'content_left');
    }, 100);
});



你可能感兴趣的:(js,倒计时)