JSFiddle - React, Tailwind, and code Playground

by antixrist

HTML

<script src="https://raw.githubusercontent.com/antixrist/waiter.js/master/waiter.js"></script>

JavaScript

var testIt = false;
setTimeout(function () {
  testIt = true;
}, 1200); // 1.2 sec

var timeout = 2000; // 2 sec
var interval = 200 // 0.2 sec

// run waiter!
waiter(timeout, interval, function (elapsedTime, iteration, cb) {
  console.log('check start!', 'testIt:', testIt, '; elapsed time:', elapsedTime +'ms', '; iteration:', iteration);

  var tstart = waiter.getTimeInMs();

  // async
  window.setTimeout(function () {
    console.log('check end!', 'testIt:', testIt, '; elapsed time:', (elapsedTime + Waiter.getTimeInMs() - tstart) +'ms', '; iteration:', iteration);
    var result = (testIt === true);

    if (result) {
      result = ['custom argument 1', 'custom argument 2'];
    }

    // return condition result
    cb(result);
  }, 100);
}, function (err, elapsedTime, iteration, customArg1, customArg2) {
  // customArg1 == 'custom argument 1'
  // customArg2 == 'custom argument 2'
  if (err) {
    // throw err;
    console.log('Fail! Time left but condition still "false"!', 'testIt:', testIt, ';', elapsedTime +'ms', 'and', iteration, 'iteration');
    return;
  }

  console.log('done!', 'testIt:', testIt, ';', elapsedTime +'ms', 'and', iteration, 'iteration');
});