Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何优化Javascript使其更短_Javascript_Html_Css_Optimization - Fatal编程技术网

如何优化Javascript使其更短

如何优化Javascript使其更短,javascript,html,css,optimization,Javascript,Html,Css,Optimization,我对编码还不熟悉,我希望优化这个JavaScript代码? 你的建议是什么?我应该使用一个变量来计算小时数吗? 我怎么能试着不重复同样的内容,或者把它缩短 var currentTime=new Date().getHours(); 如果(0==当前时间){ document.body.className=“sky-gradient-00”; } else if(1==当前时间){ document.body.className=“sky-gradient-01”; } ... else if

我对编码还不熟悉,我希望优化这个JavaScript代码? 你的建议是什么?我应该使用一个变量来计算小时数吗? 我怎么能试着不重复同样的内容,或者把它缩短

var currentTime=new Date().getHours();
如果(0==当前时间){
document.body.className=“sky-gradient-00”;
}
else if(1==当前时间){
document.body.className=“sky-gradient-01”;
}
...
else if(22==当前时间){
document.body.className=“sky-gradient-22”;
}
else if(23==当前时间){
document.body.className=“sky-gradient-23”;
}
html,
身体{
身高:100%;
宽度:100%;
}
.sky-gradient-00,
.天空梯度-24{
背景:#00000c;
}
.sky-gradient-01{
背景:线性梯度(至底部,#02011185%,#191621100%);
}
.天空梯度-22{
背景:线性梯度(至底部,#090401 50%,#4B1D06 100%);
}
.天空梯度-23{
背景:线性梯度(至底部,#00000 C 80%,#150800 100%);
}

如您所说,使用变量。只需使用小时数,并在少于10小时时将其填充

if(currentTime < 10) currentTime = "0" + currentTime;
document.body.className = "sky-gradient-" + currentTime;
if(currentTime<10)currentTime=“0”+currentTime;
document.body.className=“天空梯度-”+当前时间;

如您所说,使用变量。只需使用小时数,并在少于10小时时将其填充

if(currentTime < 10) currentTime = "0" + currentTime;
document.body.className = "sky-gradient-" + currentTime;
if(currentTime<10)currentTime=“0”+currentTime;
document.body.className=“天空梯度-”+当前时间;

如您所说,使用变量。只需使用小时数,并在少于10小时时将其填充

if(currentTime < 10) currentTime = "0" + currentTime;
document.body.className = "sky-gradient-" + currentTime;
if(currentTime<10)currentTime=“0”+currentTime;
document.body.className=“天空梯度-”+当前时间;

如您所说,使用变量。只需使用小时数,并在少于10小时时将其填充

if(currentTime < 10) currentTime = "0" + currentTime;
document.body.className = "sky-gradient-" + currentTime;
if(currentTime<10)currentTime=“0”+currentTime;
document.body.className=“天空梯度-”+当前时间;
var currentTime=new Date().getHours();
var cls=“天空梯度-”;
如果(当前时间<10){
cls=cls+“0”+当前时间;
}否则{
cls+=当前时间;
}
document.body.className=cls;
var currentTime=new Date().getHours();
var cls=“天空梯度-”;
如果(当前时间<10){
cls=cls+“0”+当前时间;
}否则{
cls+=当前时间;
}
document.body.className=cls;
var currentTime=new Date().getHours();
var cls=“天空梯度-”;
如果(当前时间<10){
cls=cls+“0”+当前时间;
}否则{
cls+=当前时间;
}
document.body.className=cls;
var currentTime=new Date().getHours();
var cls=“天空梯度-”;
如果(当前时间<10){
cls=cls+“0”+当前时间;
}否则{
cls+=当前时间;
}
document.body.className=cls;

你能告诉我为什么我应该使用“if(currentTime<10)”?@Wally-这部分总是有两位数的值。。。(把零加到左边)你能告诉我为什么我应该使用“if(currentTime<10)”?@Wally-那部分总是有两位数的值。。。(把零加到左边)你能告诉我为什么我应该使用“if(currentTime<10)”?@Wally-那部分总是有两位数的值。。。(把零加到左边)你能告诉我为什么我应该使用“if(currentTime<10)”?@Wally-那部分总是有两位数的值。。。(将零添加到左侧)