Zouhair
Zouhair

Reputation: 651

Run javascript function each time

I need to run any js function each 1 second(for example), i want to make my functions as parameters. to run them any time.

My code is:

var refresh = function(callback, period) {
    return window.setInterval(function() {
        callback;
    }, period); 
};

var mFunction = function() {
    console.log(new Date());
};

and to run that

refresh(mFunction, 1000);

but i can't get it.

any help please.

Upvotes: 0

Views: 484

Answers (1)

Amit
Amit

Reputation: 46323

Why so complicated:

var mFunction = function() {
    console.log(new Date());
};

var refresh = window.setInterval; // <- Just an alias, not really needed.

refresh(mFunction, 1000);

The same could be written as:

window.setInterval(function() {
    console.log(new Date());
}, 1000);

Upvotes: 1

Related Questions