Maria Jane Maria Jane - 3 months ago 20
jQuery Question

naming redundancy in javascript function parameters

How do I set default value of timing param?

function active_timer(timing){
interval = setInterval(function(){
console.log('interval');
}, timing);
}


I can do like this

function active_timer(timing){
var time = 1000;
if(timing){
time = timing
}
interval = setInterval(function(){
console.log('interval');
}, time);
}


My problem is with the naming, timing and time actually are the same thing, how can I write less redundant code in javascript?

Answer

An argument is a (local) variable just like any other.

timing = timing || 1000;

You can overwrite it without a care in the world.

Comments