2012-12-15 18 views
1

我正在试验javascript程序,并且遇到了一个障碍。程序突然滞后于我的浏览器(也许是无限循环),不知道为什么。Javascript'console'无限循环

function fullscreen() { 
    if (document.body.requestFullScreen) {document.body.requestFullScreen();} 
    else if (document.body.webkitRequestFullScreen) {document.body.webkitRequestFullScreen();} 
    else if (document.body.mozRequestFullScreen) {document.body.mozRequestFullScreen();} 
} 

var bash = document.createElement('span'); 
bash.setAttribute('id', 'bash'); 
document.body.appendChild(bash); 

var cursor = document.createElement('span'); 
cursor.setAttribute('id', 'bashCursor'); 
cursor.textContent = '_'; 
cursor.style.display = 'none'; 
cursor.style.fontWeight = 'bold'; 
document.body.appendChild(cursor); 

window.Bash = {}; 
window.Bash.printing = false; 
window.Bash.queue = Array(); 
window.Bash.span = bash; 
window.Bash.span.cursor = cursor; 

delete bash; delete bash; 


function bashPrint() { 
    window.Bash.writing = true; 
    var bash = window.Bash.span 
    var i; 
    while (window.Bash.queue.length) { 
     if (window.Bash.queue[0] == undefined) { 
      i = 0; 
      while (i < window.Bash.queue.length) { 
       window.Bash.queue[i] = window.Bash.queue[i+1]; 
       console.log('l:'+window.Bash.queue.length); 
       console.log(window.Bash.queue); 
       delete window.Bash.queue[i+1]; 
       window.Bash.queue.splice(i,1); 
       i++; 
      } 

     } else if (window.Bash.queue[0]['type'] == 'instant') { 
      bash.textContent += window.Bash.queue[0]['value']; 
      delete window.Bash.queue[0]; 
      window.Bash.queue.splice(0,1); 

     } else if (window.Bash.queue[0]['type'] == 'wait') { 
      setTimeout(bashPrintWaiting, window.Bash.queue[0]['wait']); 
      break; 

     } else if (window.Bash.queue[0]['type'] == 'cursor') { 
      if (window.Bash.queue[0]['value']) { 
       window.Bash.span.cursor.style.display = 'inline'; 
      } else { 
       window.Bash.span.cursor.style.display = 'none'; 
      } 
     } 
    } 
    window.Bash.writing = false; 
} 

function bashPrintWaiting() { 
    window.Bash.writing = true; 
    var bash = window.Bash.span; 
    bash.textContent += window.Bash.queue[0]['value']; 
    delete window.Bash.queue[0]; 
    window.Bash.queue.splice(0,1); 
    window.Bash.writing = false; 
    setTimeout(bashPrint, 0); 
} 

function bashWrite(string) { 
    var array = Array(); 
    array['type'] = 'instant'; 
    array['value'] = string; 
    window.Bash.queue[window.Bash.queue.length] = array 
} 

function bashPause(times, string) { 
    if (!string) {string='';} 
    while (times > 0) { 
     var array = Array(); 
     array['type'] = 'wait'; 
     array['value'] = string; 
     array['wait'] = 50 + Math.floor(Math.random()*450); 
     window.Bash.queue[window.Bash.queue.length] = array; 
     times--; 
    } 
} 

function bashCursor(enabled) { 
    var array = Array(); 
    array['type'] = 'cursor'; 
    array['value'] = enabled; 
    window.Bash.queue[window.Bash.queue.length] = array; 
} 

bashWrite('Uncompressing'); 
bashPause(12, '.'); 
bashWrite('OK\n'); 

bashPause(3); 
bashWrite('Build v. 0.1.01-release (x86_64-pc)\n'); 

bashPause(2); 
bashWrite('Connecting'); 
bashPause(35, '.'); 
bashWrite('Error, unknown user. See connect.log for futher information.\n'); 

bashPause(2); 
bashWrite('[email protected] ~ $ >>'); 
bashCursor(true); 

bashPrint(); 

我上传它的jsfiddle - 之间http://jsfiddle.net/uQcCP/

程序冻结:

bashWrite('Error, unknown user. See connect.log for futher information.\n'); 

bashPause(2); 

请,你能帮帮我吗?非常感谢。

+3

明显的警告:JSFiddle会挂起你的浏览器。 – Dogbert

+2

总是在问题本身**中发布相关代码**,不要只是链接。 http://meta.stackexchange.com/questions/118392/add-stack-overfow-faq-entry-or-similar-for-putting-code-in-the-question –

+0

这就是为什么你有一个开放的Chrome方便.. 。:P –

回答

4

无限循环开始于第51行:while (window.Bash.queue.length) {

然后它74行结束if语句,并在这个队列中是从来没有缩短:

else if (window.Bash.queue[0]['type'] == 'cursor') { 
    if (window.Bash.queue[0]['value']) { 
     window.Bash.span.cursor.style.display = 'inline'; 

如果你发现自己有Chrome中的无限循环问题,打开您的开发工具并在打开页面之前转到脚本选项卡。打开页面并开始循环后,可以单击暂停按钮在代码当前正在执行的位置抛出一个断点。从那里可以更容易地预测出现错误的地方。

+0

哦。谢谢!如此愚蠢的错误:) – m93a

+0

@ m93a不客气!如果您感兴趣,我添加了一些关于如何调试的信息。 –

+0

不知道暂停按钮。非常好,另外! – qooplmao