thisTime

by Olayinka Otuniyi

JavaScript

//this one

//var thisTime = "";      
function timeSQL(time, date) {
  var thisTime = new Date().toLocaleTimeString('en-GB');
  return thisTime;
}

function dateSQL(date) {
  var today = new Date();
  var date = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
  return date;
}

var updateTimes = new DeltaTimer(
  function(time) {
    timeSQL();
    dateSQL();
  }, 500);
updateTimes.start();

//setTimeout(function () {
console.log([timeSQL(), dateSQL()]);
//}, 1000)


function DeltaTimer(render, interval) {
  var timeout;
  var lastTime;

  this.start = start;
  this.stop = stop;

  function start() {
    timeout = setTimeout(loop, 0);
    lastTime = +new Date;
    return lastTime;
  }

  function stop() {
    clearTimeout(timeout);
    return lastTime;
  }

  function loop() {
    var thisTime = +new Date;
    var deltaTime = thisTime - lastTime;
    var delay = Math.max(interval - deltaTime, 0);
    timeout = setTimeout(loop, delay);
    lastTime = thisTime + delay;
    render(thisTime);
  }
}