Javascript setTimeout issue w/ for loop

前端 未结 3 653
天命终不由人
天命终不由人 2021-01-28 02:32

This is my function, when called the related node turns red and then does nothing.
Here is the javascript:

function blink (node, flickers)
{
    originalColo         


        
3条回答
  •  -上瘾入骨i
    2021-01-28 03:05

    i is "i when the anonymous function is called" not "i when setTimeout is called".

    You need to create a closure and pass the current value of i into it.

    function ChangeColorLater(i) {
        return function () {
            ChangeColor (node, (((i%2) == 0) ? (originalColour) : ('red')))
        }
    }
    
    setTimeout (ChangeColourLater(i), (i*200));
    

提交回复
热议问题