简体   繁体   中英

Call object literal method using setInterval not working

I'm trying to call a method on an object literal using setInterval and can't get it to work. I know I have some kind of context problem and found several other posts discussing the topic but couldn't find the information I needed to get my code working.

Thank you.

var viewModel = {
        displaySomething: function () { console.log('displaying something'); },
        displaySomethingElse: function () { console.log('displaying something else');}
    };

    setInterval(viewModel.displaySomething(), 60000);
    setInterval(viewModel.displaySomethingElse(), 60000);

Take the () out of your function call.

setInterval(viewModel.displaySomething, 60000);

You can read all about it here Javascript setInterval not working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM