JSFiddle - React, Tailwind, and code Playground

by sejoker

HTML

<div id="result"></div>

JavaScript

var el = document.getElementById('result');
var appendLog = function( res ){
    el.innerHTML += res + '<br>';
}

appendLog("Static Argument, changed within function (expected: 0, 1, 0, 1):");
var fStatic = function( i ) {
  var fPrintArg = function() {
    appendLog( i );
  };
  setTimeout(fPrintArg, 100);
  fPrintArg();
}; 
fStatic(0);
fStatic(1);

// Wait for the first example to complete
setTimeout(function(){
  appendLog("<br>Dynamic Argument, function invoked with different argument(expected: 1, 0):");
  var fDynamic = function( i ) {
    if( i == 0 ) {
      // Delay execution in order to let i change to 1
      setTimeout( function() {
        appendLog(i);
      }, 100);
    } else {
      appendLog(i);
    }
  }; 
  fDynamic(0);
  fDynamic(1);
}, 200);