计算计数器计算经过的时间

时间:2016-10-10 06:09:56

标签: javascript jquery server

我正在尝试创建一个计数柜台或票证,显示" X已保存的金额"经过的时间。

这是我到目前为止所提出的问题并没有问题,但一旦页面刷新后它会重新设置为起始编号。

var xmlHttp;
function srvTime(){
    try {
        //FF, Opera, Safari, Chrome
        xmlHttp = new XMLHttpRequest();
    }
    catch (err1) {
        //IE
        try {
            xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (eerr3) {
                alert("AJAX not supported");
            }
        }
    }

    xmlHttp.open('HEAD',window.location.href.toString(),false);
    xmlHttp.setRequestHeader("Content-Type", "text/html");
    xmlHttp.send('');
    return xmlHttp.getResponseHeader("Date");
}

var tick1 = 0;
var tick2 = 0;
var tick3 = 0;

function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}

function updateTicker() {
    $("#counter1").html(function (index, oldhtml) {
        tick1 += (6/5);
        return numberWithCommas( Math.floor(tick1) );
    });

    $("#counter2").html(function (index, oldhtml) {
        tick2 += (4.5/5);
        return numberWithCommas( Math.floor(tick2) );
    });

    $("#counter3").html(function (index, oldhtml) {
        tick3 += ((100/60)/5);
        return numberWithCommas( Math.floor(tick3) );
    });
}


var st = srvTime();
var date = new Date(st);
var tick = Math.floor((date.getTime() - 1349829008000) / 20000000);

tick1 = Math.floor(tick * (6/5));
tick2 = Math.floor(tick * (4.5/5));
tick3 = Math.floor(tick * ((100/60)/5));

$("#counter").html("" + numberWithCommas( tick1 ));
$("#counter2").html("" + numberWithCommas( tick2 ));
$("#counter3").html("" + numberWithCommas( tick3 ));

setInterval(function() {updateTicker();}, 60000);

0 个答案:

没有答案