JS倒计时

<script>
                            // 初始化KISSY
                            var S = KISSY;
                            S.config({
                                debug : true
                                // 开启debug模式
                            });
                            S.use("node,overlay", function (S, Node, O) { 
                                //微博发布 投票微博
                                var voting_${vtList.voteId } = new O.Dialog({
                                    srcNode: ".vote_${vtList.voteId }",
                                    mask:true,
                                    align:{
                                        points:['cc', 'cc']
                                    }
                                });
                                
                                //投票
                                $('.addvote_${vtList.voteId }').on("click", function () {
                                    doShow(${vtList.voteId });//计算显示倒计时
                                    voting_${vtList.voteId }.show();//显示投票、结果界面
                                });
                            });
                            
                            
                            //投票倒计时JS 开始 
                            var endTime,time_now_server,time_now_client,time_end,time_server_client,timerID,ids,time_minite=0;
                            
                            //写成doShow方法并传参,解决同一个圈子中有几个已投票时,查看倒计时时,显示的时间都相同的问题
                             function doShow(idss){
                                 
                                 ids = idss;//为了在show_time方法中获取ID
                                 
                                endTime = $("#endTime_"+idss).val();//获取投票结束时间
                                
                                //获取结束的时间 方法一:有用户获取不到time_end
                                //time_end = new Date(endTime);
                                
                                //获取结束的时间 方法二
                                //解决不同用户在同一个圈子中查看投票结果时,有用户获取不到time_end的问题
                                var year_et = endTime.substring(0,4);//年 2014-08-26 22:22:22
                                var month_et = endTime.substring(5,7);//
                                var day_et = endTime.substring(8,10);//
                                var hour_et = endTime.substring(11,13);//
                                var minute_et = endTime.substring(14,16);//
                                var second_et = endTime.substring(17,19);//
                                
                                if(month_et<10){
                                    month_et = month_et.substring(1,2);
                                }
                                if(day_et<10){
                                    day_et = day_et.substring(1,2);
                                }
                                if(hour_et<10){
                                    hour_et = hour_et.substring(1,2);
                                }
                                if(minute_et<10){
                                    minute_et = minute_et.substring(1,2);
                                }
                                if(second_et<10){
                                    second_et = second_et.substring(1,2);
                                }
                                
                                //alert(day_et+" "+hour_et+" "+minute_et+" "+second_et);
                                time_end = new Date(year_et,month_et-1,day_et,hour_et,minute_et,second_et);
                                time_end = time_end.getTime(); 
                                
                                //alert("#endTime_"+idss+"--"+endTime+"--"+time_end);
                                time_now_server = new Date();//开始的时间  
                                time_now_server = time_now_server.getTime();  
                                
                                time_now_client = new Date();  
                                time_now_client = time_now_client.getTime();  
                                
                                time_server_client = time_now_server - time_now_client; //时间差 
                                   
                                setTimeout("show_time()",1000);//倒计时
                            }
                            
                               //显示倒计时
                            function show_time() { 
                                
                                var timer = document.getElementById("timer_"+ids); //获取ID 
                                if(!timer){  
                                    return ;  
                                }  
                                timer.innerHTML = time_server_client;  
                            
                                var time_now,time_distance,str_time;  
                                var int_day,int_hour,int_minute,int_second;  
                                var time_now=new Date();  
                                time_now = time_now.getTime()+time_server_client;  
                                time_distance = time_end-time_now; //时间差
                                
                                 //alert(time_end+"--"+time_now);
                                 
                                if(time_distance>0) {  
                                    int_day = Math.floor(time_distance/86400000);  
                                    time_distance-=int_day*86400000;  
                                    
                                    //小时
                                    int_hour = Math.floor(time_distance/3600000);  
                                    time_distance-=int_hour*3600000; 
                                    
                                    //分钟
                                    int_minute = Math.floor(time_distance/60000);  
                                    time_distance-=int_minute*60000;  
                                    
                                    //
                                    int_second=Math.floor(time_distance/1000);  
                                    
                                    if(int_hour<10)  int_hour="0"+int_hour;  
                                    if(int_minute<10) int_minute="0"+int_minute;  
                                    if(int_second<10)  int_second="0"+int_second;  
                                    
                                    str_time = int_day+"天"+int_hour+"小时"+int_minute+"分钟"+int_second+"秒";  
                                    timer.innerHTML = str_time;  
                                    setTimeout("show_time()",1000);  
                                } else {  
                                    //timer.innerHTML = timer.innerHTML;  
                                    timer.innerHTML = "<a class='grey'>已到期</a>";  
                                    clearTimeout(timerID);  
                                }  
                            } 
                            //投票倒计时JS 结束
                        </script>

 

posted @ 2014-08-27 15:49  mingyue1818  阅读(335)  评论(0编辑  收藏  举报