开发者

Passing Parameters to onExpiry Function in jQuery Countdown plugin

So I'm using http://keith-wood.name/countdown.html to do a countdown and am trying to figure out how to pass some params to the callback function that can be set in the plugins' options:

 var param = 5;    
 initCount(param);

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

I took a look at the full version of jquery.countdown.js and found the develo开发者_StackOverflowper literally saying this on Line 47 and Line 48: "// receives no parameters and 'this' is the containing division" Well, that's not good enough for me. Applying his options, he uses the following code: (Line 209)

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

So.... What would be the best way to change:

onExpiry.apply(target, [])

So that I could pass my params if needed in the options as proposed above?

Thoughts?


I tried the solution that was marked "correct" and it did not work. I sent an email to the author of the Countdown plug-in and his response is below. I implemented his response in my solution and it worked perfectly! Here is the correct way to do it:


You can pass extra parameters via an anonymous function. Something like this:

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

Cheers

Keith


By the way, here is the working code from my application:

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);
}


i have encoutered the very same probleme , thanks god i just found a round about solution here it is : you have to add a new parameter to the signature of options in 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:''
    };

then you should add your identifier the call of the function

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

then the call to your UpdateCountdown will look like this

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

your function will look like this one :

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

you can potentialy turn alwaysExpire on the Jquery.countdown.js to true , so you can test pre-expired dates .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜