In ES2015 (aka "ES6") and later, you can specify a default value for the argument:
// ES2015+
function active_timer(timing = 1000) {
// -------------------------^^^^^^^
interval = setInterval(function(){
console.log('interval');
}, timing);
}
In ES5 and earlier (well, technically all of this applies to ES2015+ as well): You can assign values to function arguments, so you don't need a separate variable unless you want one. If active_timer is called without any arguments, timing will have the value undefined.
You have a few options for how to determine you need to apply the default:
if (timing === undefined) {
timing = 1000;
}
or
if (typeof timing === "undefined") {
timing = 1000;
}
or
if (arguments.length === 0) { // NOT RECOMMENDED
timing = 1000;
}
(Not recommended because using arguments within a function can impact its performance.)
Or if you know active_timer will never be called with 0, you can use the curiously-powerful || operator:
function active_timer(timing){
interval = setInterval(function(){
console.log('interval');
}, timing || 1000); // Only if you know timing won't be 0
}
The result of a || b is the value of a if it' truthy, or the value of b if a is falsy. The falsy values are 0, "", undefined, null, NaN, and of course, false.
interval = setInterval(function() { ... }, timing || 1000);