function initJMCLMap() { if (typeof jmclib.JMCLocator === 'object' && document.getElementById('jmcl-map') !== null) { jmclib.JMCLocator.initJMCLMapFunction(); } else { setTimeout(initJMCLMap, 100); } } var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyBy6dF5lcJaAzoLHo7xqogrNy3EuRd0tMs&callback=initJMCLMap&libraries=places'; document.head.appendChild(script); window.onload = function() { var extra = ''; if (window.jQuery) { // jQuery is loaded } else { // jQuery is not loaded extra = '&include=jquery'; } var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://jmclocator.com/embed/js/jmclocator.js?cache_buster='+Date.now()+extra; document.head.appendChild(script); } var jmcLocatorTimeout = 1000000; // 1000000ms = 1000 seconds var jmclib = function() {}; // Let's create an empty object JMCLocator = { embed: function(elementId, options){ ensureJMCLocatorBuilderIsSet(jmcLocatorTimeout).then(function(){ jmclib.JMCLocator.embed(elementId, options); }); } } // This is the promise code, so this is the useful bit function ensureJMCLocatorBuilderIsSet(timeout) { var start = Date.now(); return new Promise(waitForJMCLocatorBuilder); function waitForJMCLocatorBuilder(resolve, reject) { if (window.JMCLocator && window.jmclib.JMCLocator) resolve(window.jmclib.JMCLocator); else if (timeout && (Date.now() - start) >= timeout) reject(new Error("timeout")); else setTimeout(waitForJMCLocatorBuilder.bind(this, resolve, reject), 30); } }