Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!