Skip to content

Instantly share code, notes, and snippets.

@kayode-adechinan
Created March 9, 2020 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayode-adechinan/75c1f7c32cd4633a023f740d86df3075 to your computer and use it in GitHub Desktop.
Save kayode-adechinan/75c1f7c32cd4633a023f740d86df3075 to your computer and use it in GitHub Desktop.
const loadDynamicScript = (src, callback) => {
const existingScript = document.getElementById('scriptId');
if (!existingScript) {
const script = document.createElement('script');
script.src = src; // URL for the third-party library being loaded.
//script.id = 'libraryName'; // e.g., googleMaps or stripe
document.body.appendChild(script);
script.onload = () => {
if (callback) callback();
};
}
if (existingScript && callback) callback();
};
state = {
csReady:false,
ftReady:false,
interval: undefined
}
componentDidMount(){
this.interval = setInterval(() => {
if(this.state.csReady == false || this.state.ftReady == false){
loadDynamicScript('js/common_scripts.min.js', () => {
this.setState({ csReady: true });
});
loadDynamicScript('js/functions.js', () => {
this.setState({ ftReady: true });
});
}
}, 1);
}
componentWillUnmount() {
clearInterval(this.interval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment