JSFiddle - React, Tailwind, and code Playground

by winns

JavaScript

$(document).ready(function(){

	var	fileSize = 100000, // bytes
		uploadedBytes = 0,
		uploadedPercent = 0,
		uploadedTickAgo = 0,
		bytesPerSecond = 0,
		statusText = '',
		timer = null;

	function onUpload(){
		uploadedBytes += Math.floor(Math.random() * 500) + 2500;
		uploadedPercent = (uploadedBytes * 100 / fileSize).toFixed(2);
		
		bytesPerSecond = uploadedBytes - uploadedTickAgo;
		uploadedTickAgo = uploadedBytes;
		
		statusText = 'Uploaded '+ uploadedBytes +' out of '+ fileSize +' bytes ('+ uploadedPercent +'%)<br>';
		
		// speed in B/s
		//statusText += 'Upload speed '+ bytesPerSecond +' B/s';
		
		// speed in kB/s
		statusText += 'Upload speed '+ (bytesPerSecond / 1024).toFixed(2) +' kB/s';
		
		if (timer == null){
			timer = setInterval(onUpload, 1000);
		}
		if (uploadedBytes >= fileSize){
			clearInterval(timer)
			statusText += '<br>Upload complete..';
		}
		
		$('body').html( statusText );
	}
	onUpload();

});