2012-03-07 101 views
1

好了,所以我使用的是jquery countdown plugin和聲明如下正在重置計數器,每30秒

$j(document).ready(function() { 
$j('#clock').countdown({ to : "#{event.start_date.strftime("%Y:%m:%d:%H:%M")}", remaining : "#{event.start_date - Time.now}", display_on_complete : 'list', hide_on_complete : 'countdown' }); 
}); 

這個工作正常,但每隔30秒我需要從這個AJAX調用更新這個倒計時隨着時間的推移

$j(document).ready(function() { 
    function refresh() { 
    $j.ajax({ 
    type: "GET", 
    url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json", 
    dataType: "jsonp", 
    success: function(data){ 
    unix_timestamp = data.Result.Timestamp 
    var json_date = new Date(unix_timestamp*1000); 
    console.log(json_date); 
    setTimeout(refresh, 30000); 
    } 
}); 

正如你可以看到日期是json_date變量.....,我需要更換Time.now在倒計時的方法,每30秒......就如何實現這一目標

+0

我沒有看到你從內部任何地方調用refresh()函數嗎?我知道你想循環它,但它看起來並不像你已經初始化它。 – 2012-03-07 04:27:44

+0

我看不出您的代碼有問題,唯一可能的問題可能是上下文。試着將你的函數定義爲'window.refresh = function(){...};'看看事情是否有不同的表現。此外,您的代碼缺少一組'});' – rkw 2012-03-07 07:48:13

回答

2

任何想法jQuery-countdown-Timer插件不支持設置任意剩餘時間。

此外,該插件不需要remaining作爲選項。 (也沒有display_on_complete或hide_on_complete`選項,如果你鏈接到錯誤的插件,請告訴我。)

假設你想要插件有任意的剩餘時間是你關心的,你必須修改該插件。

我會改變線路42〜50的jquery.countdown-timer.js到:

if (options.remaining) { 
    seconds_remaining = options.remaining; 
} else { 
    // Split the specified date/time string into an array 
    var date_elements = options.to.split(':'); 

    // Create Date objects for start and end date/times 
    var end_date = new Date (date_elements[0], date_elements[1] - 1, date_elements[2], date_elements[3], date_elements[4]); 
    var start_date = new Date(); 

    // Calculate number of seconds remain until end date/time 
    seconds_remaining = (end_date.getTime() - start_date.getTime()) * .001; 
} 

,並調用倒計時功能的AJAX調用中的下列方式:

function refresh() { 
    $j.ajax({ 
     type: "GET", 
     url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json", 
     dataType: "json", // your url doesn't have a callback, no need for jsonp 
     success: function(data){ 
      unix_timestamp = data.Result.Timestamp 
      var json_date = new Date(unix_timestamp*1000); 

      $j('#clock').countdown({ remaining: (event.start_date - json_date) * .001 }); 

      setTimeout(refresh, 30000); 
     } 
    }); 
} 

這是假設event對象在refresh函數範圍內,$j是你的jQuery對象。

我可能完全誤解了你的問題,所以讓我知道我是否。

+0

我認爲它可能已被修改......這裏是鏈接http://pastie.org/3541077 – Trace 2012-03-07 13:25:55