简体   繁体   English

时间功能,找到更好的解决方案

[英]Time function, find a better solution

I have made this time and date function with a bunch of if else statements. 我使用了一系列if else语句来实现此时间和日期功能。 But is there a better way to do this? 但是有更好的方法吗? I think it uses a lot of processor power. 我认为它使用了大量的处理器功能。

The function increments time. 该功能增加时间。 Each number is a var. 每个数字都是一个变量。 So in seconds we have single seconds (sec) and tens of seconds (tensec). 因此,以秒为单位,我们只有一秒(秒)和数十秒(十秒)。

check out the jsfiddle here: http://jsfiddle.net/MLgbs/1/ 在此处查看jsfiddle: http//jsfiddle.net/MLgbs/1/

$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);

Why don't you just use the Date() object? 您为什么不只使用Date()对象? Rather than all this calculation, simply pull the time from it at regular intervals (several per second, say), and display that - that way, it will be synced to the time on the client computer, rather than the inaccurate setInterval function, which will probably not give an accurate reflection of the time after very long (especially given all the legwork you're making it do with a dozen or so nested conditions!) 与其进行所有计算,不如简单地以固定的时间间隔(例如每秒几秒钟)从时间中提取时间,并显示该时间-这样,它将被同步到客户端计算机上的时间,而不是不准确的setInterval函数,很长一段时间后,可能无法准确反映时间(尤其是考虑到您要进行的所有腿部工作,它们都在十几个嵌套条件下完成!)

If you have multiple users who all require reference to a common clock, use PHP to get the date instead - this will return the server date/clock, instead of the client. 如果您有多个都需要引用公共时钟的用户,请改用PHP获取日期-这将返回服务器日期/时钟,而不是客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM