coffeescript:从文件夹中依次显示图像

时间:2011-11-23 00:16:38

标签: coffeescript

我是CoffeeScript和JavaScript的新手,因此我在更改src标记的<img>属性时,在html文件中按顺序显示某些图片时遇到了一些问题。这是我的代码:

#START: Loop to show all the images of the folder
i = 0
show_images = (folder, start_idx, end_idx) ->
  if start_idx <= end_idx
    $('#video')
      .attr 'src', "/media/generated#{folder}generated#{start_idx}.jpg"
    i += 1
    setTimeout ->
      show_images(folder, start_idx+i, end_idx)
    , 50
#START: Loop to show all the images of the folder

我认为这是失败的,因为CoffeeScript作为JavasCript不会同步运行,但是我无法弄清楚如何完成所需的任务。

1 个答案:

答案 0 :(得分:2)

您不需要i var。试试这个:

#START: Loop to show all the images of the folder
show_images = (folder, start_idx, end_idx) ->
  if start_idx <= end_idx
    $('#video')
      .attr 'src', "/media/generated#{folder}generated#{start_idx}.jpg"
    setTimeout ->
      show_images(folder, start_idx+1, end_idx)
    , 1000
#START: Loop to show all the images of the folder