I have this Javascript code that works as expected:
<div class="test"></div> <script> setTimeout(function(){$(".test").append("test1")},1000); setTimeout(function(){$(".test").append("test2")},2000); </script> <script src="js/jquery.min.js"></script>
First it shows "test1", and then "test2" after a second, as such: "test1test2", which is what I want.
When I try to do this in a FOR loop, for example:
var timeInterval = 1000; for (var i = 0, l = 2; i < l; i++ ) { setTimeout(function(){$(".test").append("test" + i)},timeInterval); timeInterval += 1000; }
Then I first get "test2" and then "test2" after a second, as such: "test2test2", which I don't want.
In fact, if l = 3, I get "test3test3test3" instead of "test1test2test3". Does anyone know how to solve this problem?
source share