Javascript 时间函数,找到更好的解决方案

Javascript 时间函数,找到更好的解决方案,javascript,jquery,performance,time,Javascript,Jquery,Performance,Time,我用一堆if-else语句创建了这个时间和日期函数。但是有没有更好的方法呢?我认为它使用了大量的处理器能力 该函数增加时间。每一个数字都是一个变量。因此,以秒为单位,我们有一秒(秒)和十秒(秒) 在这里查看JSFIDLE: $seconds=$('.seconds.one'); $tenseconds=$('.seconds.ten'); $minutes=$('.minutes.one'); $tenminutes=$('.minutes.ten'); $hours=$('.hours.one

我用一堆if-else语句创建了这个时间和日期函数。但是有没有更好的方法呢?我认为它使用了大量的处理器能力

该函数增加时间。每一个数字都是一个变量。因此,以秒为单位,我们有一秒(秒)和十秒(秒)

在这里查看JSFIDLE:

$seconds=$('.seconds.one');
$tenseconds=$('.seconds.ten');
$minutes=$('.minutes.one');
$tenminutes=$('.minutes.ten');
$hours=$('.hours.one');
$tenhours=$('.hours.ten');
$days=$('.days.one');
$tendays=$('.days.ten');
$months=$('.months.one');
$tenmonths=$('.months.ten');
$years=$('.years.one');
$tenyears=$('.years.ten');
$houndredears=$('.years.houndredears');
var-sec=0;
var tensec=0;
var min=0;
var tenmin=0;
var小时=0;
var十小时=0;
风险值天数=0;
var-tendays=0;
var月数=0;
var十个月=0;
var年数=0;
var十年=0;
var Houndredears=0;
功能时钟(){
//秒
如果(第9节){
sec++;
log($秒,秒);
}否则{
秒=0;
log($秒,秒);
//时差

如果(tensec为什么不直接使用
Date()
对象呢?与其进行所有这些计算,只需定期(比如每秒几次)从中提取时间即可,并以这种方式显示-这样,它将与客户端计算机上的时间同步,而不是不准确的
setInterval
函数,该函数可能无法在很长时间后准确反映时间(特别是考虑到您在十几个嵌套条件下所做的所有工作!)


如果您有多个用户都需要引用一个公共时钟,请使用PHP来获取日期-这将返回服务器日期/时钟,而不是客户端。

setInterval
不可靠。使用对象!这是一个很好的方法。但是,如果每个数字都发生变化,我不需要检查并记录它们吗?问题是当一个数字改变时,n被触发。“console.log(selector,int);”应该是“animate(selector,int)”你在制作什么动画?很难说你没有看到更多的代码,但是如果元素的位置是由它们的值决定的,你可以调用动画函数,不管它们的值如何-如果值没有改变,它就不会制作动画-这会帮你避免堆积条件。这行得通吗?我正在制作一个翻转时钟的动画。但是动画函数n只添加和删除类,动画本身是用css完成的。这回答了你的问题吗?我想我明白你的意思了-仍然,而不是很多条件,你可以将时钟值存储在一个对象中,从服务器/日期对象更新,然后遍历它,看看是否有任何更改。如果有,你就有一个字符串键可以参考所需的动画/课程。这有意义吗?好的,很酷,我现在没有接下来几天的时间,但我回伦敦后会检查这个问题,如果问题没有解决,我会为你做一个适当的检查。。。
$seconds = $('.seconds .one');
$tenseconds = $('.seconds .ten');
$minutes = $('.minutes .one');
$tenminutes = $('.minutes .ten');
$hours = $('.hours .one');
$tenhours = $('.hours .ten');
$days = $('.days .one');
$tendays = $('.days .ten');
$months = $('.months .one');
$tenmonths = $('.months .ten');
$years = $('.years .one');
$tenyears = $('.years .ten');
$houndredyears = $('.years .houndred');

var sec = 0;
var tensec = 0;
var min = 0;
var tenmin = 0;
var hours = 0;
var tenhours = 0;
var days = 0;
var tendays = 0;
var months = 0;
var tenmonths = 0;
var years = 0;
var tenyears = 0;
var houndredyears = 0;


function clock(){
    //Seconds
    if(sec < 9){
        sec++;
        console.log($seconds, sec);
    } else {
        sec = 0;
        console.log($seconds, sec);
        //Tenseconds
        if(tensec<5){
            tensec++;
            console.log($tenseconds, tensec);
        } else {
            tensec = 0;
            console.log($tenseconds, tensec);

            //minutes
            if(min<9){
                min++;
                console.log($minutes, min);
            } else {
                min = 0;

                console.log($minutes, min);
                //tenminutes
                if(tenmin<5){
                    tenmin++;

                    console.log($tenminutes, tenmin);
                } else {
                    tenmin=0;

                    console.log($tenminutes, tenmin);
                    //hours
                    if(hours<9 && (tenhours*10+hours<23)){
                        hours++;

                        console.log($hours, hours);
                    } else {
                        hours=0;

                        console.log($hours, hours);
                        //tenhours
                        if(tenhours<2 && (tenhours*10+hours<23)){
                            tenhours++;
                            console.log($tenhours, tenhours);
                        } else {
                            tenhours=0;
                            console.log($tenhours, tenhours);
                            if(days < 9 && (tendays*10+days<30)){
                                days++;
                                console.log($days, days);
                            } else {
                                if(days !== 0){
                                    days = 0;
                                    console.log($days, days);
                                }
                                if(tendays<2){
                                    tendays++;
                                    console.log($tendays, tendays);
                                } else {
                                    tendays = 0;
                                    console.log($tendays, tendays);
                                    if(months<9 && (tenmonths*10+months<11)){
                                        months++;
                                        console.log($months, months);
                                    } else {
                                        months = 0;
                                        console.log($months, months);
                                        if(tenmonths<0){
                                            tenmonths++;
                                            console.log($tenmonths, tenmonths);
                                        } else {
                                            tenmonths = 0;
                                            console.log($tenmonths, tenmonths);
                                            if(years < 9){
                                                years++;
                                                console.log($years, years);
                                            } else {
                                                years = 0;
                                                console.log($years, years);
                                                if(tenyears<9){
                                                    tenyears++;
                                                    console.log($tenyears, tenyears);
                                                } else {
                                                    tenyears = 0;
                                                    console.log($tenyears, tenyears);
                                                    if(houndredyears<9){
                                                        houndredyears++;
                                                        console.log($houndredyears, houndredyears);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}
setInterval(function(){clock();},1000);