4 Replies Latest reply on Apr 5, 2011 3:18 AM by flexillu10

    Filename variable changes too fast

    flexillu10 Level 1

      This problem is hard to explain so please bare with me.

       

      My app is a file downloader.

       

      I loop through a list of filenames and call my download function on each one.

       

      Once the download function has been called on item[i], the loop carries on and calls the download function on item[i+1], item[i+2] etc resulting in a queue situation.

       

      However this means that by the time the first file is being written to disk, the filename getter has already looped through and is on the final file name in the list.

       

      This results in all files except the last being overwritten, and thus only the final file is saved to disk.

       

      Basically what i need is a way to pause the for loop (i know this sound silly) until the download functions are complete, but i can't find a way to add a listener to a function. Do i need to bring all these functions into one perhaps?

       

      Its a lot of code to post so here's a quick layout of how it currently downloads files.

       

      function downloadButtonClickHandler{
      
           for each item in list {
                downloadHTTPService.send(item.fileName)
           }
      
      }
      
      function downloadHTTPServiceresult {
           downloadFile(URL)
      }
      
      function downloadFile(URL){
           //download logic, obviously a delay here
           getFileName()
           writeFile()
      }
      
      function getFileName{
           returns filename
      }
      
      function writeFile(){
      
           //write logic
           resolve path "/foldera/folderb/"+ downloadFileName
      }
      
      

       

      So by the time writeFile is called downloadFileName has already changed value. e.g file1, file2, file3 so when the first file is written to disk the name file 3 is used, and the the same happens for the rest of the files so essentially i am getting 3 files all named the same thing.

       

      Thanks for reading, i really appreciate any advice.