JS function encapsulation to achieve a countdown effect

Time:2021-2-27

1: The core algorithm is the input event – the remaining time, which is the countdown

Total number of milliseconds entered – the total number of milliseconds of the current time. The remaining number of milliseconds is the countdown time

2: Turn the remaining milliseconds into days, hours, minutes and seconds

 

function getTime(time){

Var nowtime = + new date(); / / the number of milliseconds of the current time is returned

Var inputtime = + new date (time); / / the number of milliseconds entered is returned

Var times = (inputtime – nowtime) / 1000; / / times is the time remaining converted to seconds

Var days = parseInt (times / 60 / 60 / 24) / / declare variable storage days

      days = days<10?‘0’+days:days;

Var hours = parseInt (times / 60 / 60% 24) / / declare variable storage hours

      hours = hours<10?‘0’+hours:hours;

Var mins = parseInt (times / 60% 60) / / declare variable storage minutes

      mins = mins<10?‘0’+mins:mins;

Var scons = parseInt (times% 60) / / life will store the current seconds

      Scons = Scons<10?‘0’+Scons:Scons;

Return ‘distance’ + days +’days’ + hours +’hours’ + minutes +’minutes’ + scons +’seconds’;

}

 

    console.log(getTime(‘2020-10-1 19:00:00’));