2012-07-19 44 views
0

嘿在這篇文章中的2個問題,他們可能很簡單,你有經驗的js球員:-)檢查手機中的文件目錄

首先;爲什麼當我傳遞它時,「文件名」在readEntries內部未定義?

秒;爲什麼總是這樣,當目錄是空的時候?

繼承人我的代碼:我打電話getPicturepath與像「women.png」字符串。

function getPicturePath(filename){ 
    alert(filename); //is correct 
    var reader = DATADIR.createReader(); 
    reader.readEntries(function(entries, filename){ 
    alert(filename);//is undefined ??? 
     var doWeHaveIt = function(entries,filename){ 
      checkForFile(entries,filename) 
      }; 
     if(doWeHaveIt){ 
      alert('allready have: '+DATADIR.fullPath+filename); 

     } else { 
      alert('need to download file: '+filename); 
     } 
    },onError); 
} 

function checkForFile(entries,filename){ 
    console.log("The dir has "+entries.length+" entries."); 
    if(entries.indexOf(filename)!=-1){ 
     alert(filename+' allready exists'); 
     return true; 
    } else { 
     alert(filename+" doesn't exists"); 
     return false; 
    } 
} 

回答

1
reader.readEntries(function(entries, filename){ 

這是定義參數entriesfilename功能。

例如,該功能可能會做這樣的事情:

readEntries: function(callback) { 
    // do something, then 
    callback(some, datas); 
} 

如果你只是想在這個函數中使用filename,只是使用它。就像這樣:

function getPicturePath(filename){ 
    alert(filename); //is correct 
    var reader = DATADIR.createReader(); 
    reader.readEntries(function(entries){ 
     alert(filename);// is still correct 

第二部分(總是如此)是因爲這樣:

function hi() {} 

if (hi) { 
    // You're always getting there. 
} 

我寫的是什麼正是你做了什麼。我讓你猜如何糾正:-)

+0

好吧,我明白了,我是一個可怕的@ JS。你能告訴我如何從函數checkIfFile ....返回true/false嗎? – 2012-07-19 10:54:31

+1

不,我不想爲你編碼。我寧願讓你學習一些關於JS的東西。像函數聲明或函數實例化一樣。此外,這真是基礎。閱讀我在聊天室向你展示的教程將告訴你如何在不到一個小時的時間裏學習。 – 2012-07-19 10:57:04

+0

好的,非常感謝 – 2012-07-19 10:58:16