I want to call a function in a cycle with setTimeout()
, and also pass the counter as a parameter. Here's what I tried:
<!DOCTYPE html>
<html><body>
<button onclick="timed1()">Display alerts</button>
<script>
function timed1() {
for (i=1; i<4; i++){
setTimeout(
(function(i){ alert(i) })(i), 1000*i
);
}
}
</script>
</body></html>
Function calls are done with correct values, but immediately instead of the intervals I wanted. I have read this and this SO questions but unfortunately don't see yet what should I change in my case.