cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <section class="demo">
	<button class="js-click">Click this button to change that  <code>div</code> &rarr;</button>

<div class="js-find changeable">
	&larr; This <code>div</code> will change when you click  that button 
</div>
	<output class="js-out output">This <code>output</code> will change when that &larr; <code>div</code> eventually changes</output>
</section>

<section class="summary">	
	<header class="summary__header">
	<p>Why are you doing this? </p>
	<p>How does this work?</p>
	<p>What is a good way to experience inception in a web page? </p>
	</header>
	<p>Get the answers to all of these questions <a href="http://blog.frankmtaylor.com/2017/06/16/promising-a-mutation-using-mutationobserver-and-promises-together/">with this blog post</a>.</p>

<p>Or be lame, and just go to this <a href="https://gist.github.com/paceaux/8f8d5d1a57409c5b5f5f2519ceb8ac83">gist</a>, if you just want the code.</p>
	
</section>


            
          
!
            
              body {
	font-family: Helvetica, arial, sans-serif;
}
.summary__header {
	font-weight: 700;
	font-size: 1.5em;
}
.summary__header p {
	line-height: 1;
	margin: .5em 0;
}
.demo {
	display: flex;
	justify-content: space-around;
}
.output, .changeable {
	padding: .618em;
	font-size: 1.618em;
	border: 2px solid;
}

.output {
	border-color: #6688aa
}


.activated {
	font-weight: 700;
	background: rgba(180,220,230,.3);
}

.output {
	
}
            
          
!
            
              console.clear();

/*DEMO STUFF*/
const button = document.querySelector('.js-click');
const find = document.querySelector('.js-find');
const out = document.querySelector('.js-out');

button.addEventListener('click', (evt)=>{
	find.classList.add('activated')
});

/*PROMISE STUFF*/


/** AttrPromise
* @param {element} DOM element. required
* @param {attributeName} String. Optional. Attribute that is expected to change.
* @param {rejectTime} Int. Optional. Seconds (not ms) to wait before rejecting. 0 means there is no reject time.
* @returns {promise}
*/
function attrPromise(element, attributeName,rejectTime = 0) {
	return new Promise((resolve,reject) => {
		let hasChanged = false;
		const observerConfig = {attributes: true,attributeOldValue:true};		
		const observer = new MutationObserver((mutations) => {
			mutations.forEach((mutation) => {
				// if attributeName is given and we have a matching change, disconnect and resolve
				if (attributeName && mutation.attributeName == attributeName) {
					hasChanged = true;
					observer.disconnect();
					resolve(element, element.getAttribute(attributeName));
				}
				
				// no attributeName is given, but some attribute in general changed, disconnect and resolve
				if (!attributeName) {
					hasChanged = true;
					observer.disconnect();
					resolve(element);
				}
			});
		});
		
		// Add a reject time if provided. Doesn't use recurrsion, but should it?
		if (rejectTime > 0) {
			window.setTimeout(()=>{
				if (!hasChanged) {
					reject(element);
				}
			},rejectTime * 100);
		}
		
		if (attributeName) observerConfig.attributeFilter =[attributeName];
		observer.observe(element, observerConfig);
	});
}
// use the attribute promise!
attrPromise(find,'class').then((element, attributeValue) => {
	out.innerText = 'I changed because the div got an attribute change';
}).catch((element)=> {
	out.innerText = 'The div never changed!';
});

            
          
!
999px
Loading ..................

Console