0

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.

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.