2013-04-24 88 views
0

我想寫至少10​​行文件,使用循環在文件中使用PhoneGap追加多行,但每次我在下面運行此代碼時,能夠將1行添加到文件中作爲換行符。但10行不打印10行。請建議我如何使用phoneGap javascript糾正我的問題。無法在Javascript中使用循環寫入10行文件(PhoneGap)

function gotFileWriter(writer) { 

        for (var z = 0; z<= 10; z++) { 

         console.log("normal write success"); 

         writer.onwrite = function(evt) { 

          console.log("write success"); 
         }; 


         writer.onwriteend = function(evt) { 

          console.log("write end"); 
         } 

         writer.seek(writer.length); 
         writer.write("id,updated,created,fname,lname,company,tags,type,label,value,source,sourceid\r\n"); 
         writer.abort(); // I tried even commenting 
        } 
} 

回答

0

這奏效了,我

構建一個大的字符串:

function gotFileWriter(writer) 
{ 
    var bigString = ''; 
    for (var i=0; i<numberOfLines; i++) 
    { 
    bigString += line[i]; 
    bigString += '\r\n'; 
    } 
... 

然後做大規模寫入文件:

... 
    writer.onwrite = function (evt) { ... }; 
    writer.onwriteend = function (evt) { ... }; 
    writer.seek(writer.length); 
    writer.write (bigString); 
} 
0

你試試這個嗎?

function gotFileWriter(writer) { 

    console.log("normal write success"); 

    writer.onwrite = function(evt) { 
     console.log("write success"); 
    }; 


    writer.onwriteend = function(evt) { 
     console.log("write end"); 
    } 


    for (var z = 0; z< 10; z++) { 

     writer.seek(writer.length); 
     writer.write("id,updated,created,fname,lname,company,tags,type,label,value,source,sourceid\r\n"); 
     writer.abort(); // I tried even commenting 
    } 
} 
0

您可以用這個將文本附加到文件而不是循環播放

function gotFileWriter(writer) { 
    writer.seek(writer.length); 
    writer.write(" - append- \n"); 
}