How to Use setTimeout in a for…loop

后端 未结 4 1155
北恋
北恋 2021-01-16 11:35

What I want is this (news ticker type functionality):

  1. Get a list of li\'s from a ul tag
  2. Loop through all the li\'s and get the text
  3. display t
4条回答
  •  死守一世寂寞
    2021-01-16 12:11

    The reason this is happening is because of closures. The for loop block has closures around it, so when you reference 'li_text' it was always equal the last value that li_text was set to. The for loop does not create a separate closure for each iteration through the loop.

提交回复
热议问题