//倒计时,分为时间格式和数字格式倒计时。带有具体格式
/*使用示例
$("#countDown").on("click",function(){
MY.countDown.getTime({
seconds:5,//倒计时数字
flag:"#countDown",//页面标识
type:"num",//倒计时类型
action:function(){//倒计时为0时可以执行的方法
$("#countDown").html("执行完毕");
}
});
});
*/
/*
*para
* 倒计时的秒数:obj.seconds
* 返回数据类型:obj.type (time:时间类型,num:数字类型)
* 插入文档中的位置:obj.flag
* */
;(function(){
var __countDown = {
getTime:function(obj){
var lt = [],day1,hour,minute,second;
var seconds = parseInt(obj.seconds);
var flag = obj.flag;
var type = obj.type;
if(type === "num"){//以数字形式展现出来的倒计时。
if(!$(flag).data("go") && $(flag).data("go") != "go"){
$(flag).data("go","go");
}else{
console.log("含有'data-go'属性,倒计时已经在执行");
return false;
}
$(flag).text(__countDown.formatTimeNum(seconds));
var countsId = setInterval(function(){
seconds--;
seconds = __countDown.formatTimeNum(seconds);
if(seconds == "00"){
if(obj && obj.action){
obj.action();
}else{
$(flag).text("00");
}
$(flag).removeAttr("data-go");
clearInterval(countsId);
}else{
$(flag).text(seconds);
}
},1000);
}else{//以时间形式展现的倒计时
if(!$(flag).data("go") && $(flag).data("go") != "go"){
$(flag).data("go","go");
}else{
console.log("含有'data-go=go'属性,倒计时已经在执行");
return false;
}
var leftTime = __countDown.leftTimeToArr(seconds);
$(flag).html("<span>" + leftTime["day"] + "天 </span>"+"<span>" + leftTime["hour"] + ":</span><span>" + leftTime["minute"] +":</span><span>" + leftTime["second"] + "</span>");
var intervalId = setInterval(function(){
seconds = seconds - 1;
if(seconds === 0){
if(obj && obj.action){
obj.action();
}else{
$(flag).html("<span>0天</span><span>00:</span><span>00:</span><span>00</span>");
}
$(flag).removeAttr("data-go");
clearInterval(intervalId);
}else{
var leftTime = __countDown.leftTimeToArr(seconds);
$(flag).html("<span>" + leftTime["day"] + "天 </span>"+"<span>" + leftTime["hour"] + ":</span><span>" + leftTime["minute"] +":</span><span>" + leftTime["second"] + "</span>");
}
},1000);
}
},
leftTimeToArr:function(seconds){
var lt = [],day1,hour,minute,second; day1=Math.floor(seconds/(60*60*24)); lt["day"] = day1; hour=Math.floor((seconds-day1*24*60*60)/3600); hour = __countDown.formatTimeNum(hour); lt["hour"] = hour; minute=Math.floor((seconds-day1*24*60*60-hour*3600)/60); minute = __countDown.formatTimeNum(minute); lt["minute"] = minute; second=Math.floor(seconds-day1*24*60*60-hour*3600-minute*60); second = __countDown.formatTimeNum(second); lt["second"] = second; return lt; }, formatTimeNum:function(num){ if(parseInt(num) < 10){ num = "0" + num; } return num } }; TC.countDown = __countDown ||{};})();