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.
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.
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.
you can put it into tag using 'onload' event:
or just use jQuery:
$(document).ready(function() {
yourFunct();
});
By calling your function in page on-load
<script>
window.onload = startcountdown;
function startcountdown(){
//write your code here
}
</script>