2011-08-12 75 views
1

所以我用http://keith-wood.name/countdown.html做一個倒計時,我試圖找出如何一些PARAMS傳遞給可以在插件選項中設置的回調函數傳遞參數onExpiry功能:jQuery的倒計時插件

var param = 5;  
initCount(param); 

function initCount(param) { 
    $('selector').countdown({ 
     onExpiry: 
     function endCount(param) { 
     alert(param); 
     } 
    }); 
} 

我看了一下jquery.countdown.js的完整版本,發現開發人員在第47行和第48行這樣說:「//不接收任何參數,'這是包含的分隔」 ,這對我來說還不夠好。應用他的選項,他使用以下代碼:(Line 209)

var onExpiry = this._get(inst,'onExpiry'); (onExpiry){ onExpiry.apply(target,[]); }

所以....會是什麼改變的最佳途徑:

onExpiry.apply(目標,[])

所以,我通如果需要上述建議的選項,我的參數?

想法?

回答

4

我已經encoutered非常相同的probleme,感謝上帝,我只是找到了一個輪關於這裏的解決方案是: 你有一個新的參數添加到選項中Jquery.countdown.js簽名:

this._defaults = { 
     until: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count down to 
      // or numeric for seconds offset, or string for unit offset(s): 
      // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds 
     since: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count up from 
      // or numeric for seconds offset, or string for unit offset(s): 
      // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds 
     timezone: null, // The timezone (hours or minutes from GMT) for the target times, 
      // or null for client local 
     serverSync: null, // A function to retrieve the current server time for synchronisation 
     format: 'dHMS', // Format for display - upper case for always, lower case only if non-zero, 
      // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds 
     layout: '', // Build your own layout for the countdown 
     compact: false, // True to display in a compact format, false for an expanded one 
     significant: 0, // The number of periods with values to show, zero for all 
     description: '', // The description displayed for the countdown 
     expiryUrl: '', // A URL to load upon expiry, replacing the current page 
     expiryText: '', // Text to display upon expiry, replacing the countdown 
     alwaysExpire: true, // True to trigger onExpiry even if never counted down 
     onExpiry: null, // Callback when the countdown expires - 
      // receives no parameters and 'this' is the containing division 
     onTick: null, // Callback when the countdown is updated - 
      // receives int[7] being the breakdown by period (based on format) 
      // and 'this' is the containing division 
     tickInterval: 1 ,// Interval (seconds) between onTick callbacks 
     identifier:'' 
    }; 

那麼你應該添加標識符的函數的調用

if (onExpiry) { 
        onExpiry.apply(target,[this._get(inst, 'identifier')]); 
       } 

然後調用你的UpdateCountdown看起來就像這樣

$('#defaultCountdown).countdown({identifier:1,onExpiry: yourFuncion, until: new Date('2012-01-04')}); 

你的函數看起來像這樣的:

function yourFunction(id){ 
alert(id); 
} 

可以potentialy在Jquery.countdown.js轉alwaysExpire爲true,所以你可以測試預過期日期。

+1

真好!做得好! – spez86

+0

我正在等待你的反應;)我很高興它最終變得有用。 ;) – Genjuro

5

我嘗試了標記爲「正確」的解決方案,但沒有奏效。我發送了一封電子郵件給Countdown插件的作者,他的回覆如下。我在我的解決方案中實現了他的迴應,並且它完美地工這是正確的方法:


您可以通過匿名函數傳遞額外的參數。事情是這樣的:

$('#countdown').countdown({until: +300, onExpiry: function() { 
    myFunction(createdTime, searchText, updateDestination); 
}});  

乾杯

基思


順便說一句,這裏是從我的應用程序的工作代碼:

function MakeTimer(timerNode, displayNode, searchTerm){ 
    // get the duration of the timer 
    var duration = Number($("#refreshTimer").val()); 

    // create a new property and/or set property to creation time 
    timerNode.prop('createdTime', new Date()); 
    timerNode.prop('searchTerm', searchTerm); 

    //assumes that timerNode is a jQuery node and duration is an int 
    //timerNode.countdown({until: duration, format: "s", compact: true, onExpiry: resetTimer}); 
    timerNode.countdown({until: duration, format: "s", compact: true, onExpiry: function() { 
     resetTimer(timerNode, displayNode); 
    }}); 

    // get twitter data 
    getTwitterData(timerNode, displayNode); 
} 

function resetTimer(timerNode, displayNode){ 
    // get the current duration of the timer 
    var duration = Number($("#refreshTimer").val()); 
    timerNode.countdown('change','until', duration); 

    // get updated twitter data 
    getTwitterData(timerNode, displayNode); 
}