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>In this demo Throttle function is used to handle repeated button clicks.</p>
					<p>
						First, Click the button, wait for few seconds for the side effect action to complete, then click on it again.</p>
					<p>Then try rapidly clicking the button.</p>
					<p>Notice how the two counts change differently from our orignal example, and from the De-bounce example</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 API_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();
	throttleUpdatePreview();
});

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

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

function throttle(fn, delay) {
	let wait = false;
	return () => {
		if (!wait) {
			wait = true;
			setTimeout(() => {
				fn();
				wait = false;
			}, delay);
		}
		// if (timer) clearTimeout(timer);
		// timer = setTimeout(() => {
		// 	fn();
		// }, delay);
	};
}

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

const throttleUpdatePreview = throttle(updatePreview, 550);

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

              
            
!
999px

Console