2014-01-22 101 views
0

*代碼改寫Nodejs,redis自動完成,關閉

我試圖將antirez的autocompleter轉換爲javascript來與node.js一起使用它。

我無法通過關閉問題。也許有人可以幫助我。

我看到問題出在哪裏,但無法弄清楚如何解決它們。

的while循環發射,突破是不正確的,將無法正常工作等

代碼:

var acp = { 

    vars: { 

    dict  : false, 
    count : false, 
    prefix : false, 
    rangeLen : 10, 
    start : false, 
    results : false, 
    range : false, 

    }, 

    sendResults: function(){ 
    console.log(acp.vars.results); 
    }, 

    getzrank: function(){ 

    redis.zrank(acp.vars.dict, acp.vars.prefix, function(err, start){ 

     acp.vars.start = start; 

    }); 

    }, 

    getzrange: function(){ 

    redis.zrange(acp.vars.dict, acp.vars.start, parseFloat(acp.vars.start+acp.vars.rangeLen-1), function(err, range){ 

     acp.vars.range = range; 

    }); 

    }, 

    createAutocomplete: function(){ 

    while(acp.vars.results.length != acp.vars.count){ 

     acp.getzrange(); 

     acp.vars.start+=acp.vars.rangeLen; 

     if(! acp.vars.range || acp.vars.range.length == 0) { break; } 

     for (var i in acp.vars.range){ 

     var minLen = Math.min(i.length,acp.vars.prefix.length); 

     if(i.substr(0, minLen) != acp.vars.prefix.substr(0, minLen)){ 

      acp.vars.count = acp.vars.results.length; 

     } 

     if(i.substr(0, i.length-1) == "*" && acp.vars.results.length != acp.vars.count) { 

      acp.vars.results.push(i.substr(0,-1)); 

     } 

     } 

    } 

    acp.sendResults(); 

    }, 

    init: function(dict,prefix,count){ 

    acp.vars.range = false; 

    acp.vars.dict = dict; 
    acp.vars.prefix = prefix; 
    acp.vars.count = count; 
    acp.vars.results = []; 

    acp.vars.start = acp.getzrank(); 

    if(! acp.vars.start){ acp.sendResults(); } 
    else{ acp.createAutocomplete(); } 

    } 

} 

回答

0

break語句是while循環的範圍之內,因爲它嵌套在匿名函數。不知道這是否能解決您的問題。

+0

我知道。我想這可能是解決方案。但如何解決它? – user2429266

+0

我改寫了代碼。沒有修復錯誤,但也許這可以讓其他人更容易幫助。 – user2429266