/** * Define a jquery timing plug-in that implements recording timing start time, end time, total elapsed function * @param $ * * @author Ivan [email protected] * @date November 25, 2014 PM 8:48:5 5 * @version V1.0 **/(function($) {$.timer= {}; /** * Expand the Date object to add a formatting method to it * @param format incoming date formats, such as YYYY-MM-DD Hh:mm:ss * @returns*/Date.prototype.format=function(format) {varo = { "m+": This. GetMonth () + 1,//Month"D+": This. GetDate (),// Day"H +": This. GetHours (),//Hour"m+": This. getminutes (),//minute"S+": This. getseconds (),//Second"q+": Math.floor (( This. GetMonth () + 3)/3),//Quarter"S": This. Getmilliseconds ()//Millisecond }; if(/(y+)/. Test (format)) format= Format.replace (Regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); for(varKincho)if(NewRegExp ("(" + K + ")"). Test (format)) format= Format.replace (regexp.$1, regexp.$1.length = = 1?)O[k]: ("XX" + o[k]). substr ("" +O[k]) (length)); returnformat; }; varInterval = 0;//Define a timer //Private Function: Start Timer function functionrun (ET) {interval= SetInterval (Chat, "+", ET);//timed Settings } //Private Function: Timed execution function functionChat (ET) {varD =NewDate (). Format (' Yyyy-mm-dd hh:mm:ss '); //Update End Time$("#"+ET). html (d); } //plug -in defaults$.timer.defaults ={startTime:' StartTime ', EndTime:' EndTime ', Costtime:' Costtime ' }; //Timing Starts$.timer.start =function(options) {varopts =$.extend ({}, $.timer.defaults, options); varSt =NewDate (). Format (' Yyyy-mm-dd hh:mm:ss '); $("#"+opts.starttime). html (ST); $("#" +opts.costtime). HTML (""); Chat (opts.endtime); //Start timer when loading pagerun (opts.endtime); }; //Private functions: Time to calculate statistics functionSetcosttime (opts) {varStime = $ ("#" +opts.starttime). html (); varETime = $ ("#" +opts.endtime). html (); varSdate =NewDate (Date.parse (Stime.replace (/-/g, "/"))); varEDate =NewDate (Date.parse (Etime.replace (/-/g, "/"))); varDiffmillisecond = Edate.gettime ()-sdate.gettime ();//the number of milliseconds for the time difference //calculate the difference in days varDays = Math.floor (Diffmillisecond/(24 * 3600 * 1000)); //calculate the number of hours varleave1 = diffmillisecond% (24 * 3600 * 1000);//number of milliseconds remaining after counting days varHours = Math.floor (leave1/(3600 * 1000)); //calculate the number of minutes difference varLeave2 = leave1% (3600 * 1000);//number of milliseconds remaining after hours of calculation varminutes = Math.floor (Leave2/(60 * 1000)); //calculate the number of seconds difference varLeave3 = leave2% (60 * 1000);//Count the number of milliseconds left after the number of minutes varseconds = Math.Round (leave3/1000); varCttext = "Time consuming:" + days + "Day" + Hours + "hours" + minutes + "minutes" + seconds + "seconds"; $("#"+opts.costtime). html (cttext); } //End of timing$.timer.stop =function(options) {varopts =$.extend ({}, $.timer.defaults, options); //Turn off the timercleartimeout (interval); Setcosttime (opts); }; }) (jQuery);
jquery Timer Plugin