(function() { 'use strict'; var time = function() { return moment().format('h:mm:ss a'); }; function updateChart() { d3.select('#current-seconds') .append('li') .style('width', function() { return _.random(300, 800) + 'px'; }) .html(' chart updated: ' + time()); } // have a reference of the current seconds console.log('current time in seconds'); console.log(moment().format('ss')); // update chart every minute (from random start) // base source: http://stackoverflow.com/a/14644729/1711570 function setChartInterval() { // have a reference of the current seconds var currentDateSeconds = moment().format('ss'); console.log('current time in seconds: ' + '\n' + currentDateSeconds); // PASS if initial page load is within the second half of minute if (currentDateSeconds > 30 && currentDateSeconds <= 55) { // update chart every 60 seconds setInterval(updateChart, 60000); console.log('PASSED: so refresh every 60 seconds'); console.log(moment().format('h:mm:ss a')); } else { setTimeout(function () { setChartInterval(); }, ((_.random(30, 40)) - currentDateSeconds) * 1000); console.log('FAIL: so set setTimeout (with some randomness)'); console.log(moment().format('h:mm:ss a')); } } updateChart(); setChartInterval(); })();