javascript - Countdown not working in some cases -
i'm using countdown script home page. loads @ first visit only. surf other web pages , come home page, it's not loading.
i'm using meteor , react.
componentdidmount () { $(window).load(() => { $('#clock').countdown('2016/9/9', function (event) { var $this = $(this).html(event.strftime('' + '<span>%m</span> months ' + '<span>%w</span> weeks ' + '<span>%d</span> days ' + '<span>%h</span> hr ' + '<span>%m</span> min ' + '<span>%s</span> sec')); }); }); }
what's wrong code?
it's not idea mixing react , jquery dom manipulation. in fact, doesn't make sense call $(window).load
inside of componentdidmount
because componentdidmount
ensures react component loaded dom.
here example of simple countdown timer using purely "reactive" approach. copied source code in case link becomes invalid:
var countdowntimer = react.createclass({ getinitialstate: function() { return { secondsremaining: 0 }; }, tick: function() { this.setstate({secondsremaining: this.state.secondsremaining - 1}); if (this.state.secondsremaining <= 0) { clearinterval(this.interval); } }, componentdidmount: function() { this.setstate({ secondsremaining: this.props.secondsremaining }); this.interval = setinterval(this.tick, 1000); }, componentwillunmount: function() { clearinterval(this.interval); }, render: function() { return ( <div>seconds remaining: {this.state.secondsremaining}</div> ); } }); react.rendercomponent(<countdowntimer secondsremaining="10" />, document.queryselector('#output'));
the timer dictated component's state, decremented through tick
function. tick
function polled every x
seconds using setinterval
.
i know use case more complicated since requires counting down months, weeks, days, etc specific calendar date. either code (wouldn't hard) or find appropriate npm
library you.
Comments
Post a Comment