简体   繁体   中英

I want to run a timer after getting time from firebase-database, how to decease time by one second using setInterval() in ReactJS

For auction web app I want to run a timer after getting time from firebase-database, how to decrease time by one second using setInterval() in ReactJS I tried below code that is increasing time but I want decrease

    function display()
{
    var today = new Date();
    var month = today.getMonth();
    var day = today.getDay();
    var year = today.getFullYear();

    var hour = today.getHours() > 12 ? today.getHours() - 12 : today.getHours();
    var minute = today.getMinutes();
    var seconds = today.getSeconds();


    var output = month + '/' + day + '/' + year + ' - ' +
    hour + ':' + minute + ':' + seconds + ':';
  console.log(output)


}
var timer = setInterval(display,1000)
console.log(timer)

You need to compare against some time when working with setInterval in this fashion. The time returned from the server is perfect for this. The psuedo-code is provided

setInterval(function() {
  var difference = timeFromServer - Date.now();
  console.log(formatDate(difference));
}, 1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM