本文實(shí)例講述了jQuery實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能的方法。分享給大家供大家參考,具體如下:
1.效果圖如下:
2.html代碼:
<div class="timeFix"> <div class="daojishi" id="09/04/2016 00:00:00"> <span class="timeh"></span> <span class="timem"></span> <span class="times"></span> <span class="timen"></span> </div> </div>
3.js代碼:
setInterval(lxfEndtime,60); //倒計(jì)時(shí) function lxfEndtime(){ $(".daojishi").each(function(){ //var lxfday=$(this).attr("lxfday");//用來判斷是否顯示天數(shù)的變量 var endtime = new Date($(this).attr("id")).getTime();//取結(jié)束日期(毫秒值) var nowtime = new Date().getTime(); //今天的日期(毫秒值) var youtime = endtime-nowtime;//還有多久(毫秒值) var seconds = youtime/1000;//秒 var minutes = Math.floor(seconds/60);//分 var hours = Math.floor(minutes/60);//小時(shí) var days = Math.floor(hours/24);//天 var CDay= days ; var CHour= hours % 24; var CMinute= minutes % 60; var CSecond= Math.floor(seconds%60);//"%"是取余運(yùn)算,可以理解為60進(jìn)一后取余數(shù),然后只要余數(shù)。 var c=new Date(); var millseconds=c.getMilliseconds(); var Cmillseconds=Math.floor(millseconds %100); if(CSecond<10){//如果秒數(shù)為單數(shù),則前面補(bǔ)零 CSecond="0"+CSecond; } if(CMinute<10){ //如果分鐘數(shù)為單數(shù),則前面補(bǔ)零 CMinute="0"+CMinute; } if(CHour<10){//如果小時(shí)數(shù)為單數(shù),則前面補(bǔ)零 CHour="0"+CHour; } if(Cmillseconds<10) {//如果毫秒數(shù)為單數(shù),則前面補(bǔ)零 Cmillseconds="0"+Cmillseconds; } if(endtime<=nowtime){ $(this).html("已過期")//如果結(jié)束日期小于當(dāng)前日期就提示過期啦 }else{ $(this).html("<span class='timeh'>"+CHour+"</span><span class='timem'>"+CMinute+"</span><span class='times'>"+CSecond+"</span><span class='timen'>"+Cmillseconds+"</span>"); } });
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery日期與時(shí)間操作技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。