Sheery Sheery - 1 year ago 107
Javascript Question

loop on a function in javascript?

Can we run a loop on a function in javascript, so that the function executes several times?

Answer Source

if you tired of using standard like (IF, WHILE), here is the another way of doing.. :)

you can use setTimeOut and clearTimeOut to execute functions in a certain interval. If you want to execute an function for a specific number of times, you still can acheive it by incrementing index and clearTimeOut as soon as your index reaches to certain point.

setTimeout() - executes a code some time in the future

clearTimeout() - cancels the setTimeout()

example from w3schools

<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function timedCount()

function doTimer()
if (!timer_is_on)

function stopCount()

<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download