开发者

why is there a call to a function in a variable in a jquery plugin

开发者 https://www.devze.com 2023-01-04 14:42 出处:网络
I want to try to understand the use of these constructs because I don\'t. If I see this in a plugin function configuration(user_settings) {

I want to try to understand the use of these constructs because I don't.

If I see this in a plugin

function configuration(user_settings) {
    //Override the default settings with the user settings
 defaults = { 
 time_in_seconds: 3600,
 time_format: 'ss',
 tick: function(timer, time_in_seconds, formatted_time) {},
 buzzer: function(timer) {},
 autostart: true
  };
  return $.extend(defaults, user_settings);

}

The tick is called within the plugin and setting new values but when I comment those lines out, everything still works.

What is the general idea behind it. I looked at the github page, but there is no explanation

EDIT

If the plugin iterates, it is setting the parameters of tick

settings.tick(timer, current_time, formatted_time);

How can I set a user_setting for tick, that still makes use off those parameters?

In other words, getting something in between those execution {} brackets.

EDIT I did some more testing and apparently it is possible to pass any number of parameters at any time. Just pas something like

$("#countdown_update").createTimer({time_in_seconds: ( delay? delay/1000 : minTimeBetweenUpdates/1000 ),
        tick:function(one,two){$('#test').html('test '+one+two);}});

It will give you the timer object and the time开发者_StackOverflow中文版_in_seconds in the output

thanks, Richard


It most likely still works because the plugin has default values assigned in the code already. By commenting out your lines, the plugin is reverting to default configuration.


You are removing the default value for the "tick" function when you comment it out. It probably still works because the code is smart enough to not make use of the function if it doesn't have a value.

The tick function probably gets called each time the timer iterates. The default as shown here is an empty function, so it isn't going to do anything, but you could override tick with a function that does something (for example, have it pop up an alert each time, just to see it working).


This code is extending the defaults object with the user_settings. So the user_settings will override any similar-named properties in the defaults object. However, if any of the properties in defaults do not exist in the user_settings object, these properties will be added to the user_settings object and returned to the caller (as a new object).

0

精彩评论

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

关注公众号