JSFiddle - React, Tailwind, and code Playground

by GarethElms

JavaScript

var test = false;

var executeWhen = function( condition, callback, interval, maxCount)
{
    var counter = 0;
    var test = function()
    {
        $("body").append( counter + ", ");
        counter ++;
        
        if( counter >= maxCount)
        {
            $("body").append( "<h1>Timeout</h1>");
            clearInterval( timer);
        }
        
        if( condition())
        {
            callback;
            clearInterval( timer);
            $("body").append( "<h1>Success</h1>");
        }
    }
        
    var timer = setInterval( test, interval);
}
    
var myCondition =
    function()
    {
        return test;
    }
        
executeWhen(
    myCondition, 
    $(document).click(
        function(event)
        {
            test = true;
        }),
    250, 25);