javascript - MomentJS code does not start a new week -
$(function() { function whenisreset() { var = moment.utc(); var eventstart = moment.utc().day(2).hour(9).minute(00).second(00).millisecond(00); var eventstartnewweek = moment.utc().day(-4).hour(00).minute(00).second(00).millisecond(00); var eventend = moment.utc().day(3).hour(9).minute(00).second(00).millisecond(00); var eventendnewweek = moment.utc().day(-4).hour(9).minute(00).second(00).millisecond(00); if (now.isafter(eventend)) { eventstart = eventstartnewweek; eventend = eventendnewweek; } if (now.isbefore(eventstart)) { $('#resettime').text('resets ' + eventstart.from(now)); } if (now.isafter(eventstart) && now.isbefore(eventend)) { $('#resettime').text('reset ' + eventstart.from(now)); } } whenisreset(); setinterval(whenisreset, 1000); });
i using 'momentjs'.
the above code supposed to: - set 'tuesday [9am]' start - set 'wednesday [9am]' end - if time before 'start' shows time 'start' - if time after 'start' , before 'end' show time since 'start' - if time after 'end' should set 'start' , 'end' next week. (so can repeated).
now, works. however, after event has ended, not repeat , goes blank.
this 1 clasic. set values eventstartnewweek , eventendnewweek once, , therefore value not recalculated when assign them different variable.
eventstart = eventstartnewweek; eventend = eventendnewweek;
you instance create function , call everytime need create new eventstartnewweek or eventendnewweek.
function eventstartnewweek () { return moment.utc().day(-4).hour(00).minute(00).second(00).millisecond(00); }; function eventendnewweek () { return moment.utc().day(-4).hour(9).minute(00).second(00).millisecond(00); }; // assign them if (now.isafter(eventend)) { eventstart = eventstartnewweek(); eventend = eventendnewweek(); }
Comments
Post a Comment