0

My function is a timer that should run when the page is displayed. I wrote

<script>
    startcountdown();
</script>

In html but this doesn't work. How can I call that function? I don't need special event to happen before calling it.

Sargsyan Grigor
  • 533
  • 1
  • 5
  • 22

5 Answers5

5
<script>
window.onload = function() {
   startcountdown();
}
</script> 
overburn
  • 1,194
  • 9
  • 27
3

You may use JavaScripts native window.onload function, if you are not using any other library like jQuery or something else.

<script>
  window.onload = function() {
    startcountdown();
  };
</script>

I'd also like to mention that you could also start a function by just adding it to the end of your markup. That being said, if you define a function function startcountdown() { } at the very beginning of your html, you could simply use startcountdown(); at the very end of your html (e.g. before your closing </body> tag).

This approach would simply execute your function after your DOM has being loaded, since it's defined as the last call of your markup.

Aer0
  • 3,792
  • 17
  • 33
2

you can put it into tag using 'onload' event:

or just use jQuery:

$(document).ready(function() {
    yourFunct();
});
Pardeep Pathania
  • 1,378
  • 2
  • 15
  • 23
1

you can try this put onload="startcountdown();" in html body tag

Deepesh kumar Gupta
  • 884
  • 2
  • 11
  • 29
0

By calling your function in page on-load

<script>
window.onload = startcountdown;
 function startcountdown(){
   //write your code here
}
</script> 
nisar
  • 1,055
  • 2
  • 11
  • 26