2013-07-26 80 views
0

我是建築和拍賣網站,每個產品都附有倒數計時器。拍賣網站的Javascript倒計時器

我有一個工作腳本從這裏(計算器) - >how to make countdown timer for bidding website

這是我的代碼

<script> 
    var before = "" 
    var current = "Ended" 
    var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 

    jQuery.fn.countdown = function(yr, m, d) { 
     $that = $(this); 
     theyear = yr; 
     themonth = m; 
     theday = d; 
     var today = new Date(); 
     var todayy = today.getYear(); 
     if (todayy < 1000) 
      todayy += 1900; 
     var todaym = today.getMonth(); 
     var todayd = today.getDate(); 
     var todayh = today.getHours(); 
     var todaymin = today.getMinutes(); 
     var todaysec = today.getSeconds(); 

     var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec 

     futurestring = montharray[m - 1] + " " + d + ", " + yr; 
     dd = Date.parse(futurestring) - Date.parse(todaystring); 
     dday = Math.floor(dd/(60 * 60 * 1000 * 24) * 1); 
     dhour = Math.floor((dd % (60 * 60 * 1000 * 24))/(60 * 60 * 1000) * 1); 
     dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000))/(60 * 1000) * 1); 
     dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000))/1000 * 1); 

     if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) { 
      $that.val(current); 
      return; 
     } 
     else 
      $that.val(dday + "Days, " + dhour + ":" + dmin + ":" + dsec + before); 
     setTimeout(function() { 
      $that.countdown(theyear, themonth, theday); 
     }, 1000); 
    }; 
</script> 

<input type="text" id="6" style="width: 900px"> 
<script>   
    $("#6").countdown(2011, 7, 27); 
</script> 

我的問題是我怎樣才能添加的時間呢?我不是那麼有經驗的JavaScript來操縱這與上班時間

+0

只是通過更遙遠的日期..'$(「#6」)。倒計時(2012,7,27);''會將示例日期推出1年 – user20232359723568423357842364

+0

@ user20232359723 568423357842364我的意思是我怎麼能通過一個日期時間作爲參數,並得到類似的倒計時 像..'$(「#6」)。倒計時(2012,7,27,12,10,15);' –

回答

0

得到了解決

<script> 
var before = "" 
var current = "Ended" 
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 

jQuery.fn.countdown = function(yr, m, d, h, min, s) { 
    $that = $(this); 
    theyear = yr; 
    themonth = m; 
    theday = d; 
    thehour = h; 
    theminute = min; 
    thesecond = s; 

    var today = new Date(); 
    var todayy = today.getYear(); 
    if (todayy < 1000) 
     todayy += 1900; 
    var todaym = today.getMonth(); 
    var todayd = today.getDate(); 
    var todayh = today.getHours(); 
    var todaymin = today.getMinutes(); 
    var todaysec = today.getSeconds(); 

    var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec; 
    futurestring = montharray[m - 1] + " " + d + ", " + yr + " " + h + ":" + min + ":" + s; 
    //console.log(futurestring); 
    dd = Date.parse(futurestring) - Date.parse(todaystring); 
    dday = Math.floor(dd/(60 * 60 * 1000 * 24) * 1); 
    dhour = Math.floor((dd % (60 * 60 * 1000 * 24))/(60 * 60 * 1000) * 1); 
    dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000))/(60 * 1000) * 1); 
    dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000))/1000 * 1); 

    if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) { 
     $that.text(current); 
     return; 
    } 
    else { 
     $that.text(dday + "Days " + dhour + ":" + dmin + ":" + dsec + before); 
    } 

    setTimeout(function() { 
     $that.countdown(theyear, themonth, theday, thehour, theminute, thesecond); 
    }, 1000); 
}; 
</script> 
<input type="text" id="6" style="width: 900px"> 
<script>   
$("#6").countdown(2011, 7, 27, 12, 13, 12); 
</script> 
0
var before = "" 
var current = "Ended" 
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 


jQuery.fn.countdown = function (yr, m, d) { 
    $that = $(this); 
    var delta = 0; 

    var start = function (yr, m, d) { 
     theyear = yr; 
     themonth = m; 
     theday = d; 

     var today = new Date(); 
     var todayy = today.getYear(); 
     if (todayy < 1000) todayy += 1900; 
     var todaym = today.getMonth(); 
     var todayd = today.getDate(); 
     var todayh = today.getHours(); 
     var todaymin = today.getMinutes(); 
     var todaysec = today.getSeconds(); 

     var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec 

     futurestring = montharray[m - 1] + " " + d + ", " + yr; 
     dd = Date.parse(futurestring) - Date.parse(todaystring) + delta; 
     dday = Math.floor(dd/(60 * 60 * 1000 * 24) * 1); 
     dhour = Math.floor((dd % (60 * 60 * 1000 * 24))/(60 * 60 * 1000) * 1); 
     dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000))/(60 * 1000) * 1); 
     dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000))/1000 * 1); 

     if (dday < 0 && dhour < 0 && dmin < 0 && dsec < 1) { 
      $that.val(current); 
      return; 
     } else $that.val(dday + "Days, " + dhour + ":" + dmin + ":" + dsec + before); 

     setTimeout(function() { 
      start(theyear, themonth, theday); 
     }, 1000); 
    } 
    return { 
     start: function() { 
      start(yr, m, d); 
     }, 
     addTime: function (ms) { 
      delta += ms; 
      console.log(delta); 
     } 
    } 
}; 

要使用:

var cd = $("#6").countdown(2013, 7, 28); 
cd.start(); 
cd.addTime(100000); //add time 

FIDDLE

+0

我想要將時間作爲參數遞減計數功能 –

+0

@RaymondAtivie我以爲你試圖增加時間到現有的倒計時值。在我開始之前,最初的問題還不夠清楚。 – zsong