views:

55

answers:

3

Hello,

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_in_seconds in the output

thanks, Richard

+4  A: 

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.

Tommy
ok, but could you perhaps provide an explanation why this is done.What's the use for it?
Richard
Default values are a convenience. Instead of requiring the user to set every option, the plugin designer defaults the values of a few common options. You are free to override them.
SimpleCoder
@SimpleCoder,I edited my question
Richard
+2  A: 

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).

orvado
Thank you, I am not explaining myself very good. I understand setting variables, but I don't get setting tick as a user_setting. How do you set it. Why in the variable a function? Is it some sort off callback.It's the same as with the buzzer!
Richard
+2  A: 

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).

Charles Boyung
So, those are callback functions? and the variables are beiing set in the iteration. I can understand that. How do I give it something to do within the { } brackets? Because I can understand passing a complete statement like: function(){do something}, but then I can not make use off the variables that are being set in the iteration? Hope that made sense.
Richard
You just add the key and value to the following: $("#countdown_update").createTimer({time_in_seconds: 30, tick: function(){ alert("HI!"); });
Mervyn
Or is the idea behind it to make a user setting like this$("#countdown_update").createTimer({time_in_seconds: 30,tick:function(one,two,three{dosomething})}) where the parameters are just fake values, because the real values are set in the iteration like this.->settings.tick(timer, current_time, formatted_time);
Richard
Thanks @Mervyn, but when the default setting is written like the author did, it is neccescary to use three parameters, true? Because further in the code, this statement:settings.tick(timer, current_time, formatted_time); would otherwise cause an error,right? Or is it possible to pass any number of parameters to a function in a variable at any time, no matter if they are declared or not? –
Richard
The easiest way to think about it is that you are replacing the function that is already there.
Mervyn