js倒計時簡單實現(xiàn)方法
更新時間:2015年12月17日 10:31:19 作者:eecjimmy
這篇文章主要介紹了js倒計時簡單實現(xiàn)方法,方便一些提示重要日期的來臨,感興趣的小伙伴們可以參考一下
本文實例講述了js倒計時簡單實現(xiàn)方法的代碼,分享給大家供大家參考,具體如下:
function timeDown(second) { var month = '', day = '', hour = '', minute = ''; if (second >= 86400 * 30) { month = Math.floor(second / (86400 * 30)) + '月'; second = second % (86400 * 30); } if (second >= 86400) { day = Math.floor(second / 86400) + '天'; second = second % (86400); } if (second >= 3600) { hour = Math.floor(second / 3600) + '小時'; second = second % 3600; } if (second >= 60) { minute = Math.floor(second / 60) + '分'; second = second % 60; } if (second > 0) { second = second ? second + '秒' : ''; } return month + day + hour + minute + second; }
如果想顯示倒計時效果,可以使用如下代碼調(diào)用:
<!-- 引入jquery --> <script> $(function () { var second = 10000; $('.remain_time').html(timeDown(second)); setInterval(function () { second--; $('.remain_time').html(timeDown(second)); }, 1000); }) </script> <span class="remain_time"></span>
jquery插件形式:
$.fn.timeDown = function (opt) { var second = opt.second; var tip = '已過期'; var $this = this; self._timeDown = function (second) { var month = '', day = '', hour = '', minute = ''; if (second >= 86400 * 30) { month = Math.floor(second / (86400 * 30)) + '月'; second = second % (86400 * 30); } if (second >= 86400) { day = Math.floor(second / 86400) + '天'; second = second % (86400); } if (second >= 3600) { hour = Math.floor(second / 3600) + '小時'; second = second % 3600; } if (second >= 60) { minute = Math.floor(second / 60) + '分'; second = second % 60; } if (second > 0) { second = second ? second + '秒' : ''; } else { return tip; } return month + day + hour + minute + second; }; $this.html(self._timeDown(second)); setInterval(function () { second--; $this.html(self._timeDown(second)); }, 1000) }; // 使用方式 $('.remain_time').timeDown({second:1000,tip:'已過期'})
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
Bootstrap 手風(fēng)琴菜單的實現(xiàn)代碼
這篇文章主要介紹了Bootstrap 手風(fēng)琴菜單的實現(xiàn)代碼,需要的朋友可以參考下2017-01-01JS中的數(shù)組轉(zhuǎn)變成JSON格式字符串的方法
這篇文章主要介紹了JS中的數(shù)組轉(zhuǎn)變成JSON格式字符串的方法,需要的朋友可以參考下2017-05-05js中將URL中的參數(shù)提取出來作為對象的實現(xiàn)代碼
將URL中的參數(shù)提取出來作為對象的實現(xiàn)代碼,適合希望獲取url參數(shù)的朋友。2011-08-08