Last active
August 29, 2015 14:13
-
-
Save xuanfeng/76cf2193211a3d771894 to your computer and use it in GitHub Desktop.
JS按时间区间显示不同信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 按不同的时间段显示不同的“问好”信息,一般做法是 | |
// if(hour>=0 && hour <=5;){ | |
// }else if(hour>=6 && hour <=8;){ | |
// }else if(hour>=9 && hour <=11;){} | |
// ... | |
// 这里换了一种做法,看起来相对简洁一点! | |
/* | |
Test case: | |
for(var i = 0,iMax = 23; i <=iMax;i++){ | |
console.log(i, timeHello(i)); | |
} | |
*/ | |
function timeHello(h){ | |
var t = [6,9,12,14,18,24]; | |
for(var i in t){ | |
if(h < t[i]){ | |
return ['凌晨好!','早上好!','上午好!','中午好!','下午好!','晚上好!'][i] | |
} | |
}; | |
} | |
/* | |
timeHello(new Date().getHours()) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment