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 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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console