JSFiddle - React, Tailwind, and code Playground

by Darby Rathbone

JavaScript

var aaaa = (function () {    // this funciton is only ever called once
    
    var data =Object.create(null); // so this object only gets created once in this scope
    
    
    return function (k) {  //we return this function to be the value of aaaa
        
        
        if (data.interval) clearInterval(data.interval); // check if there is a interval already set and remove if there is
    
        document.body.appendChild(document.createElement('br'));
        data.function = function () {
            document.body.appendChild(document.createTextNode(k));
        };
        data.function();   // i put this here to make it run as soon as its created rather than waiting for the first interval to tick
        data.interval = setInterval(data.function, 100);
    }
}());
setTimeout(function () {  // this is just a function executed after 2 seconds
    aaaa('test2 ');
}, 2000);
aaaa('aaaa');  // happens instantly