Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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="container-fluid">
	<h1>requestIdleCallback</h1>
	<div class="btn-group">
		<a class="btn btn-default" target="_blank" href="https://codepen.io/Krinkle/full/jywwQp">requestAnimationFrame</a>
		<a class="btn btn-default" target="_blank" href="https://codepen.io/Krinkle/full/VPWraw">setTimeout</a>
	</div>
	<hr>
	<p>
		<button type="button" class="btn btn-primary" onclick="start()">Start!</button>
		<button type="button" class="btn btn-default" onclick="startTimed()">Start (with timeout=2s)</button>
		<button type="button" class="btn btn-danger" onclick="stop()">Stop!</button>
	</p>
	<div id="status" class="alert alert-info">Average: .. | Min: .. | Max: .. | Count: ..
Longest gap: ..</div>
	<div id="error"></div>
	<pre id="log">Not started</pre>
</div>
              
            
!

CSS

              
                #log,
#status {
	white-space: pre;
}
.entry-bad {
	background-color: rgba(255, 0, 0, 0.35);
}
.entry-start {
	background-color: rgba(0, 200, 255, 0.35);
}
              
            
!

JS

              
                
var idleId;
var ricOptions;
var startDate;
var lastDuration;
var lastEnd;
var lastInterval;
var min = Math.min();
var max = Math.max();
var longestGap = 0;
var avg = 0;
var count = 0;

function work(deadline) {
	var start = performance.now();
	lastInterval = lastEnd ? start - lastEnd : false
	startDate = new Date();
	count++;
  while (deadline.timeRemaining() > 0);
	lastDuration = performance.now() - start;
	render();
  idleId = window.requestIdleCallback(work, ricOptions);
	lastEnd = performance.now();
}

function render() {
	min = Math.min(lastDuration, min);
	max = Math.max(lastDuration, max);
	longestGap = Math.max(lastInterval, longestGap);
	avg = ((avg * (count - 1)) + lastDuration) / count;
	var log = document.getElementById('log');
	var node = document.createElement('span');
	node.appendChild(document.createTextNode(
		lastInterval !== false
			? ('Ran after ' + round(lastInterval) + 'ms for ' + round(lastDuration) + 'ms - started at ' + startDate + '\n')
			: ('[Start] Ran for ' + round(lastDuration) + 'ms - started at ' + startDate + '\n')
	));
	if (lastInterval === false) {
		node.className = 'entry-start';
	} else if (lastInterval > 2000) {
		node.className = 'entry-bad';
	}
	log.prepend(node);
	if (log.childNodes.length > 500) {
		log.removeChild(log.lastChild);
	}
	document.getElementById('status').textContent =
		'Average: ' + round(avg) + 'ms | Min: ' + round(min) + 'ms | Max: ' + round(max) + 'ms | Count: ' + count + '\nLongest gap: ' + (longestGap > 3000 ? round(longestGap/1000, 1) + 's'  : round(longestGap) + 'ms');
}

function round(number, fixed) {
	return Number(number.toFixed(fixed || 0));
}

function start() {
	stop();
	ricOptions = null;
  idleId = window.requestIdleCallback(work, ricOptions);
}
function startTimed() {
	stop();
	ricOptions = { timeout: 2000 };
  idleId = window.requestIdleCallback(work, ricOptions);
}
function stop() {
  if (idleId) {
    window.cancelIdleCallback(idleId);
  	idleId = undefined;
		lastEnd = undefined;
	}
}

window.onerror = function (msg) {
	var node = document.getElementById('error');
	node.textContent = msg;
	node.className = 'alert alert-danger';
}
              
            
!
999px

Console