0

I have sample code like this

        <input type="button" value="test" onclick="newfunc()"/>
        <br/>
        <div class="first">
            <input type="text" id="iid88" />
            <br />
        </div>
    <script>
function newfunc() {
    var one = document.getElementsByClassName('first')[0].querySelectorAll("[id^=iid]");
            for (var i = 0; i < one.length; i++)
            {
            setInterval(function () {
            one[i].value = 20;
            }, 1000);
            }
}
    </script>

i cannot get answer within input text box . how can i resolve this problem ?

i see this page JavaScript closure inside loops – simple practical example but here we dont have any setInterval usage in answers

Dani
  • 33
  • 1
  • 6
  • *"i cannot get answer within input box"* what answer did you expect to get? Note that `setInterval` is **not** equivalent to a `Sleep` function in other languages – Matt Burland May 24 '17 at 20:44
  • when we click on button , this click can show to us answer in second input text box within div tag – Dani May 24 '17 at 21:01
  • The answer was marked as a duplicate, but if it'll help, here's a fiddle that uses your code and that might help you along: https://jsfiddle.net/rk2t875t/ – Joseph Marikle May 24 '17 at 21:22
  • thank you @JosephMarikle , big thank you for your help in this difficult situation , by the way could you please say me how can i find role of this line : let k = i; – Dani May 24 '17 at 21:53
  • @radolf Sure. [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) is newish to ecmascript. It allows the declaring of variables within a block scope. It has [some issues with compatibility](http://caniuse.com/#search=let), but I find it fairly safe to use. If let can't be used, you can either move the loop into the interval closure or pass it into the closure when the interval is kicked off using `bind`: https://jsfiddle.net/rk2t875t/2/ – Joseph Marikle May 24 '17 at 22:09
  • Here's one with the loop moved into the `setInterval()` closure: https://jsfiddle.net/rk2t875t/3/ – Joseph Marikle May 24 '17 at 22:11

0 Answers0