Estoy inyectando programáticamente un código de texto en el script, y necesito ejecutar un código solo después de que el script haya terminado de ejecutarse.
Mi código:
const InjectScript = (scriptContent) => { const script = document.createElement("script"); script.type = "text/javascript"; script.text = scriptContent; document.head.appendChild(script); //run some code after script executed someCode() };¿Cómo puedo comprobar si el script ha terminado de ejecutarse?
pienso asi
var IsRunning = false; function myFunction() { if (!IsRunning) { IsRunning = true; } }async await parece la mejor opción si su tiempo de ejecución está bien con la sintaxis de ECMAScript 2017, así:
const InjectScript = async (scriptContent) => { const script = await document.createElement("script"); script.type = await "text/javascript"; script.text = await scriptContent; await document.head.appendChild(script); //run some code after script executed await someCode() };