2013-05-07 55 views
0

我一直在爲特定時間和日期(即9am GMT June 17th 2013)尋找倒數計時器的腳本(最好是JavaScript)。倒計時完成後(即在9am GMT on June 17th之後),我希望網站自動重定向到另一個頁面。完成後重定向的倒數計時器

+0

你嘗試過什麼?網絡上有很多解決方案可以完成這項簡單的任務。 – Bergi 2013-05-07 13:17:31

回答

0

其實我寫的,當我們有LAN方與同事:-)

一個簡單的倒計時腳本我沒有花時間,使之成爲jQuery插件,它有一個奇怪的結構,但它的工作原理你可以根據你的需要修改它。

http://jsfiddle.net/Y6n9W/

(function($, undefined) { 
    countdown = function(settings) { 
     var _this = this, 
      defaults = { 
       end: '30.09.2012 09:00' 
      }, rDate = /(\d+).(\d+).(\d+) (\d+):(\d+)/; 

     _this.settings = $.extend(defaults, settings); 

     var matches = _this.settings.end.match(rDate); 

     _this.endDate = new Date(matches[3], matches[2] - 1, matches[1], matches[4], matches[5], 0, 0); 

     _this.nDays = $('<span />').addClass('countdown days').html('0'); 
     _this.nHours = $('<span />').addClass('countdown hours').html('0'); 
     _this.nMinutes = $('<span />').addClass('countdown minutes').html('0'); 
     _this.nSeconds = $('<span />').addClass('countdown seconds').html('0'); 

     _this.settings.element.append(
      $('<span />').append(
       _this.nDays, 
       ' Days' 
      ), 
      $('<span />').append(
       _this.nHours, 
       ' Hours' 
      ), 
      $('<span />').append(
       _this.nMinutes, 
       ' Minutes' 
      ), 
      $('<span />').append(
       _this.nSeconds, 
       ' Seconds' 
      ) 
     ); 

     countdownMethods.start.call(_this); 

     return _this; 
    }; 

    var countdownMethods = { 
     start: function() { 
      var _this = this, 
       interval = 0; 

      var updateTime = function() { 
       var now = new Date(), 
        diff = Math.round((_this.endDate - now)/1000); 

       if(diff < 0) { 
        diff = 0; 
        clearInterval(interval); 
        window.location.href = 'newurl..'; 
       } 

       var days = Math.floor(diff/86400); 

       diff -= days * 86400; 

       var hours = Math.floor(diff/3600); 
       diff -= hours * 3600; 

       var minutes = Math.floor(diff/60); 
       diff -= minutes * 60; 

       _this.nDays.html(days); 
       _this.nHours.html(hours); 
       _this.nMinutes.html(minutes); 
       _this.nSeconds.html(diff); 
      }; 

      updateTime(); 

      interval = setInterval(function() { 
       updateTime(); 
      }, 1000); 
     } 
    }; 
})(jQuery); 

並且你使用這樣的:

$(function() { 
    var counter = countdown({ 
     end: '08.05.2013 15:15', 
     element: $('div') 
    }); 
}); 
+0

太棒了!非常感謝!但有一個問題(對不起,我是一個新手),我在哪裏編輯輸入我希望它重定向到倒計時結束時的頁面地址?此次倒計時是否設置爲特定時區? – user2358381 2013-05-07 13:23:57

+0

我原來的倒計時沒有重定向功能。但是如果你看看'updateTime'函數,我添加了一個文本'newurl..',這是URL的地方,你也可以將它作爲一個參數。此倒計時功能僅使用用戶進入頁面的時區。所以,如果你需要解釋這一點,你需要讓你的服務器寫出當前的日期和時間,併爲此編寫腳本帳戶。 – thebreiflabb 2013-05-07 13:38:10

+0

完美無缺,非常感謝thebreiflabb。我非常感謝你的幫助 – user2358381 2013-05-07 13:39:55