Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="app">
	<button class="reset btn btn-dark">RESET</button>
	<div class="container p-4 ">
		<div class="row">
			<div class="col-12  mt-5">
				<div class="bg-danger text-light fs-5 p-4 text-center">
					<p>This demo simulates retriving data from an API.</p>
					<p>The button click will request data from an API and display it. This takes about 500 milliseconds</p>
					<p>First click the button, let the display update.Then try clciking the button again, Each time wait for the preview to update.</p>
					<p>Now try clicking the button as rapidly as possible, and notice how our requests pile up and the preview is updated accordingly.</p>
					<p>Also keep track of how the two counts (button click and preview update) are same.</p>
				</div>
			</div>
			<div class=" mt-3 fs-5 col-auto mx-auto">
				<button class="fetch-button btn btn-primary">
					<span class="p-2 ">Click Me</span>
					<span class="p-2 border-start">0</span>
				</button>
			</div>
			<div class=" mt-4 col-12">
				<h3 class="text-center">Data Preview updated <span id="preview-update-count">0</span> times</h3>
				<div class="data-preview  p-4 text-center fs-2 bg-dark text-light rounded-pill">

				</div>
			</div>
		</div>
	</div>
</div>
              
            
!

CSS

              
                .app {
	min-height: 100vh;
	position: relative;
}
.reset {
	position: fixed;
	left: 10px;
	top: 10px;
}

              
            
!

JS

              
                const DELAY = 500;
const resetButton = document.querySelector(".reset");

const fetchButton = document.querySelector(".fetch-button");

const count = document.querySelector("button>span:last-child");
const dataPreview = document.querySelector(".data-preview");
const previewUpdateCount = document.querySelector("#preview-update-count");
fetchButton.addEventListener("click", async () => {
	incrementButtonClickCount();
	updatePreview();
});

function incrementButtonClickCount() {
	count.innerText = +count.innerText + 1;
}

async function updatePreview() {
	const data = await fetchData();
	dataPreview.innerText = data;
	previewUpdateCount.innerText = +previewUpdateCount.innerText + 1;
}

function fetchData() {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			resolve(`Random rumber fetched from api ${Math.round(Math.random() * 100)}`);
		}, DELAY);
	});
}

resetButton.addEventListener("click", () => {
	count.innerText = 0;
	dataPreview.innerText = "";
	previewUpdateCount.innerText = 0;
});

              
            
!
999px

Console