JSFiddle - React, Tailwind, and code Playground

HTML

<div id="box"></div>

CSS

#box {
    width:100px;
    height:100px;
    background-color:blue;
}

JavaScript

var box = document.getElementById('box');

function fadeOutIn(elem, speed ) {

	if (!elem.style.opacity) {
		elem.style.opacity = 1;
	} // end if

	var outInterval = setInterval(function() {
		elem.style.opacity -= 0.02;
		if (elem.style.opacity <= 0) {
			clearInterval(outInterval);
			var inInterval = setInterval(function() {
				elem.style.opacity = Number(elem.style.opacity)+0.02;
				if (elem.style.opacity >= 1)
					clearInterval(inInterval);
			}, speed/50 );
		} // end if
	}, speed/50 );

} // end fadeOut()

fadeOutIn(box, 2000 );