Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
How to pass a parameters under onload function?
<body onload="startTime('<%= time %>')">

</body>

<script>
  setTimeout(function startTime(time) {

    alert(time);
  }, 1000) 
</script>

I have returned the "time" from the client side by input from another page. Then, the "startTime()" will be called each seconds, in which the "time" parameter will be given to the function.

How can I pass the "time" variable in the <script> in every seconds under the onload function.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

For running it every second, you have to use setInterval instead of setTimeout.

You should introduce your startTime outside of setInterval.

Your '<%= time %>' should not be a string too, it should be like this <body onload="startTime(<%= time %>)">.

<body onload="startTime(1000)">

</body>

<script>
  function startTime(time) {
    setInterval(function() {
      alert(time);
    }, time)
  }
</script>

If time is a string you need to parse your time to a number.

<body onload="startTime('1000')">

</body>

<script>
  function startTime(time) {
    setInterval(function() {
      alert(time);
    }, Number(time)) //parse time string to a number
  }
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Though your question isn't much clear, maybe you can use $('document').ready(function(){}); inside your script if you use jQuery. Also if you don't want to use jQuery another approach might be self executing anonymous function like this

<script>
(function(time) {
  //this function will we invoked immediately
  console.log(time);
})("Pass params here");
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

setTimeout will run the function after the timer (in your case 1000ms) has run out. If you want it to continuously run every N seconds, you need to use setInterval instead.

MDN: https://developer.mozilla.org/en-US/docs/Web/API/setInterval

You can pass arguments to the callback function, by passing a 3rd (or more) argument to the setInterval. But it won't be updated, you need to build your own code around it, so you get the updated value.

What you have in your code right now will not work. You don't call the callback function directly. setTimeout starts immediately after the page loads. Your startTime function is anonymous, and is essentially this:

  setTimeout((time)  => {
    alert(time);
  }, 1000) 
// alert will be run after 1 second on page load

Another recommendation is, to not run code from HTML strings. Keep JavaScript related code inside of JavaScript. You can query HTML Elements inside of JavaScript and depending on if they changed, you can start running the code.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda