The example in this article implements a countdown function. Countdown function, used in the project release time, or an activity to do countdown and so on highlighting the time function aspects.
Interface code structure, the first to complete well. This interface I do not do so beautiful, make up on the line (O (∩_∩) o haha ~).
Code Name
Here's a tutorial on JavaScript.
First set an end time, which is created by the new date (). This will be able to calculate the countdown time.
End Time
var t_endtime = new Date ("2016-05-22 00:00:00");
Then there is the conversion rule, and the conversion of the rules is commonsense.
Time conversion rule
var t_day = * *;
var t_hour = *;
var t_min = 60;
Again, the comparison of the current time is calculated by setinterval, because the time is past a second, and the acquisition of the current time is calculated in SetInterval. Of course, we have to show the calculated results to the interface.
var ele_day = document.getElementById ("Day");
var ele_hour = document.getElementById ("Hour");
var ele_min = document.getElementById ("min");
var ele_sec = document.getElementById ("SEC");
SetInterval (function () {//Get current time var t_currenttime = new Date ();
End time-current time = remaining time var t_result = T_endtime.gettime ()-t_currenttime.gettime ();
Residual time conversion var t_time = Math.floor (t_result/1000);
var t_result_day = Math.floor (T_time/t_day);
var t_result_hour = Math.floor (t_time% t_day/t_hour);
var t_result_min = Math.floor (t_time% t_day% t_hour/t_min);
var t_result_sec = Math.floor (t_time% t_day% t_hour%);
Add 0 to the value before the time is less than 10;
if (T_result_day <) {T_result_day = "0" + t_result_day;
} if (T_result_hour <) {T_result_hour = "0" + t_result_hour;
} if (T_result_min <) {T_result_min = "0" + t_result_min;
} if (T_result_sec <) {t_result_sec = "0" + t_result_sec; //Show on page ELE_DAY.TEXTCOntent = T_result_day;
Ele_hour.textcontent = T_result_hour;
Ele_min.textcontent = T_result_min;
Ele_sec.textcontent = t_result_sec;
}, 1000);
Quite simply, a countdown function is complete.
The above is the entire content of this article, I hope to help you learn.