Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
How to execute code not from string using function?

How would I execute code not from a string? Here is an example:

var ready = false;

executeWhen(ready == true, function() {
  console.log("hello");
});

function executeWhen(statement, code) {
  if (statement) {
    window.setTimeout(executeWhen(statement, code), 100);
  } else {
    /* execute 'code' here */
  }
}

setTimeout(function(){ready = true}, 1000);

Could I use eval();? I don't think so, I think that's only for strings.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You call it with code().

You need to change statement to a function as well, so it will get a different value each time you test it.

And when you call executeWhen() in the setTimeout(), you have to pass a function, not call it immediately, which causes infinite recursion.

var ready = false;

executeWhen(() => ready == true, () =>
  console.log("hello"));

function executeWhen(statement, code) {
  if (!statement()) {
    window.setTimeout(() => executeWhen(statement, code), 100);
  } else {
    code();
  }
}

setTimeout(function() {
  ready = true
}, 1000);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda