javascript - Function to refresh date every second eventually crashes Chrome -
i have few functions use date , time , push elements in dom. appears working great until after few minutes script drains chrome of memory , crashes page. here code think affecting problem:
'use strict'; // ///////////////////////////// initial /////////////////////////////////// // function leading_0( num ) { if( num < 10 ) { num = '0' + num; } return num; } // ////////////////////////////// dates //////////////////////////////////// // function getcurrenttime( date ) { // time / / / / / / / / / / / / / / / / / // var hours = date.gethours(), minutes = date.getminutes(), seconds = date.getseconds(), suffix = hours >= 12 ? 'pm' : 'am', fulltime; hours = hours % 12; if( hours === 0 ){ hours = 12; } minutes = leading_0( minutes ); seconds = leading_0( seconds ); fulltime = hours + ':' + minutes + ':' + seconds + ' ' + suffix; return fulltime; } // \\/ / / / / / / / / / / / / / / time / / / / / / / / / / / / / / / / / // function getyear( date ) { /// / / / year / / / / / / / / / / / / / / / / / // var year = date.getfullyear(); return year; } // \\/ / / / / / / / / / / / / / / year / / / / / / / / / / / / / / / / / // function getmonthday( date ) { /// month day / / / / / / / / / / / / / / / /// var day = date.getdate(); return day; } // \\/ / / / / / / / / / / / / / month day / / / / / / / / / / / / / / / /// function getmonth( date ) { // / / / month / / / / / / / / / / / / / / / / /// var months = [ 'january', 'feburary', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december' ], month = months[ date.getmonth() ]; return month; } // \\/ / / / / / / / / / / / / / / month / / / / / / / / / / / / / / / / /// function getwkday( date ) { /// / / week day / / / / / / / / / / / / / / / /// var weekdays = [ 'sunday', 'monday', 'tueday', 'wednesday', 'thursday', 'friday', 'saturday' ], wkday = weekdays[ date.getday() ]; return wkday; } // \\ / / / / / / / / / / / / / / week day / / / / / / / / / / / / / / / /// function callbysec( func ) { setinterval( func, 1000 ); } function pushdate(){ /// / / / / / push dates / / / / / / / / / / / / / / / // var today = new date(), wkday, month, day, year, time, d = document; wkday = getwkday( today ); month = getmonth( today ); day = getmonthday( today ); year = getyear( today ); time = getcurrenttime( today ); d.getelementbyid( 'wkday' ).textcontent = wkday; d.getelementbyid( 'month' ).textcontent = month; d.getelementbyid( 'day' ).textcontent = day; d.getelementbyid( 'year' ).textcontent = year; d.getelementbyid( 'time' ).textcontent = time; callbysec( pushdate ); } // \\/ / / / / / / / / / / / / / push dates / / / / / / / / / / / / / / / // // ////////////////////////////// start //////////////////////////////////// // function start() { pushdate(); } start(); <p> <span id="wkday"></span>, <span id="month"></span> <span id="day"></span>, <span id="year"></span> <b>|</b> <span id="time"></span> </p> is above code major memory hog? on page crashes chrome. there better way , same result?
take out line:
callbysec( pushdate ); at end of pushdate(). you're using setinterval() call function every second, don't need start timer again. result you're creating timer every second. after minute you're running function 60 times every second.
your code correct if callbysec used settimeout() rather setinterval().
then start() function should call callbysec:
'use strict'; function leading_0(num) { if (num < 10) { num = '0' + num; } return num; } function getcurrenttime(date) { var hours = date.gethours(), minutes = date.getminutes(), seconds = date.getseconds(), suffix = hours >= 12 ? 'pm' : 'am', fulltime; hours = hours % 12; if (hours === 0) { hours = 12; } minutes = leading_0(minutes); seconds = leading_0(seconds); fulltime = hours + ':' + minutes + ':' + seconds + ' ' + suffix; return fulltime; } function getyear(date) { var year = date.getfullyear(); return year; } function getmonthday(date) { var day = date.getdate(); return day; } function getmonth(date) { var months = [ 'january', 'feburary', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december' ], month = months[date.getmonth()]; return month; } function getwkday(date) { var weekdays = [ 'sunday', 'monday', 'tueday', 'wednesday', 'thursday', 'friday', 'saturday' ], wkday = weekdays[date.getday()]; return wkday; } function callbysec(func) { setinterval(func, 1000); } function pushdate() { var today = new date(), wkday, month, day, year, time, d = document; wkday = getwkday(today); month = getmonth(today); day = getmonthday(today); year = getyear(today); time = getcurrenttime(today); d.getelementbyid('wkday').textcontent = wkday; d.getelementbyid('month').textcontent = month; d.getelementbyid('day').textcontent = day; d.getelementbyid('year').textcontent = year; d.getelementbyid('time').textcontent = time; } function start() { callbysec(pushdate); } start(); <p> <span id="wkday"></span>, <span id="month"></span> <span id="day"></span>, <span id="year"></span> <b>|</b> <span id="time"></span> </p>
Comments
Post a Comment