Return next localized date for countdown timer

限于喜欢 提交于 2019-12-01 11:34:44

I'm wondering if there's a more optimize way to do this, particularly with the conditional statement and where I split apart the time into parameters.

Use a dispatch table to replace the conditional statement and a regexp to replace the range:

function foo(nextDate)
 {
 calcNextWebCast.nextDate.setDate(calcNextWebCast.nextDate.getDate() + (3 - 1 - nextDate.getDay() + 7) % 7 + 1);
 calcNextWebCast.nextDate.setHours(20,0,0,0);
 }

function bar(nextDate)
 {
 calcNextWebCast.nextDate.setDate(calcNextWebCast.nextDate.getDate() + (3 - 4 - nextDate.getDay() + 7) % 7 + 1);
 calcNextWebCast.nextDate.setHours(12,0,0,0);
 }


function calcNextWebCast(date) {
 calcNextWebCast.nextDate = new Date(date||new Date()); // Next webcast datetime object

 var localizedTime = new Date(date||new Date()); // localized datetime
 var today = localizedTime.getDay() + '' + localizedTime.getHours(); // format localized datetime for comparison

 var range = "012|[0][1][3-9]|[0][2-9][0-9]|[1-2][0-9][0-9]|[3][0-1][0-9]|320";

 var datemap = { "true":foo, "false":bar }

 var jump = RegExp(range).test(today);

 var go = datemap[jump];

 go();

 return nextDate;
 }

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!