Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Need help with simple code Reply To: Need help with simple code

#150097
whatsoever
Participant

In that case you might as well put your original code in the IIF, it would work.
There are a lot of ways to handle this, if your IIF returns a object, you could define ‘i’ and numLog inside the function and after that return them, like so:

var css_tricks = (function () {
    var i = 0;
    //some code

    function numLog() {
        i++;
        console.log(i);
        return i;
    }

    return {
        numLog: numLog
    }
}())

var timerId = setTimeout(function loop(){
    var i = css_tricks.numLog();
    timerId = setTimeout(loop, 100)
    if(i === 20) {
        clearTimeout(timerId)
    };
}, 100) 

It pretty much depends on the effect you are trying to achieve.