1

Почему не работает setTImeout в цикле?

    for(var j = 0; j < mass_child.length; ++j){
        name = mass_child[j].className
        if(name == undefined) continue

        if(name == mass_class_name[0] || name == mass_class_name[1] || name == mass_class_name[2]){
             mass_child[j].style.opacity = option   
             if(option == 0) {
                setTimeout(function(){
                                mass_child[j].style.display = 'none'
                            },100)

             }
          }
    }

Пробывал через замыкание, но все равно

   for(var j = 0; j < mass_child.length; ++j){
        name = mass_child[j].className
        if(name == undefined) continue

        if(name == mass_class_name[0] || name == mass_class_name[1] || name == mass_class_name[2]){
             mass_child[j].style.opacity = option   
             if(option == 0) {
                    (function(i){
                        return function(){
                            setTimeout(function(){
                                mass_child[j].style.display = 'none'
                            },100)
                        }
                    }(j))
             }
                   }}
0

1 ответ 1

1

вы с замыканиями всё правильно пробывали только ошиблись:

(function(i){
    setTimeout(function(){
        mass_child[i/*i вместо j надо*/].style.display = 'none'
    },100)
}(j))

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.