2012-11-09 119 views
0

我目前有一個表正在由一個MySQL表填充,反過來我想從這些單元中獲取數據並使用它們來創建一個模擬的實時更新,但是由於有多行im在javascript函數中使用for循環,我相信這會導致其他函數不能運行,我無法找出它的方法。運行多個函數javascript

JavaScript代碼:

var seconds = 5; 
var divid = "status"; 
var url = "boo.php"; 
var timeout; 

function refreshdiv(){ 

// The XMLHttpRequest object 

var xmlHttp; 
try{ 
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari 
} 
catch (e){ 
try{ 
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer 
} 
catch (e){ 
try{ 
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
catch (e){ 
alert("Your browser does not support AJAX."); 
return false; 
} 
} 
} 

// Timestamp for preventing IE caching the GET request 

fetch_unix_timestamp = function() 
{ 
return parseInt(new Date().getTime().toString().substring(0, 10)) 
} 

var timestamp = fetch_unix_timestamp(); 
var nocacheurl = url+"?t="+timestamp; 

// The code... 

xmlHttp.onreadystatechange=function(){ 
if(xmlHttp.readyState==4){ 
document.getElementById(divid).innerHTML=xmlHttp.responseText; 
setTimeout('refreshdiv()',seconds*1000); 
} 
} 
xmlHttp.open("GET",nocacheurl,true); 
xmlHttp.send(null); 
} 

// Start the refreshing process 

var seconds; 
window.onload = function startrefresh(){ 
setTimeout('refreshdiv()',seconds*1000); 
} 

function runningtime(int) { 
if(int == 0) { //if there is data 
console.log("int=0 so no data is present, int: " + int); 

} else if(int == 1){ 
var isRunning = new Array(); 
isRunning[0] = " "; 
for (var i=0; i < 3; i++) { 
var bool = 'running' + i; 
console.log("Running = " + bool); 
running = document.getElementById(bool).innerHTML.substring(10,11); 

console.log("running: " + running); 

if(isRunning[i] == "1") { 

var time = 'Time' + i; 

a= document.getElementById(time).innerHTML; 
console.log("a= " + a); 

    hour=a.substring(0,2); 
    min= a.substring(3,5); 
    sec= a.substring(6,8); 

    sec==sec++; 

if (min<=9) { min="0"+min; } 
if (sec<=9) { sec="0"+sec; } 

time = (hour + ":" + min + ":" + sec + " "); 

if (document.getElementById) { document.getElementById(time).innerHTML = time; } 
else if (document.layers) { 
document.layers.theTime.document.write(time); 
document.layers.theTime.document.close(); } 

} else { 
//Do nothing 
return; 
} 
} 
timeout = setTimeout("runningtime(1)", 1000); 
} 
} 

function experiencehour(exp) { 
if(exp == 0) { //if there is dexphourtexphour 
console.log("exp=0 so no data is present, exp: " + exp); 

} else if(exp == 1){ 

var isRunning = new Array(); 
isRunning[0] = " "; 
for (var i=0; i < 3; i++) { 
var bool = 'running' + i; 
console.log("Running = " + bool); 
running = document.getElementById(bool).innerHTML.substring(10,11); //checks if bot running 

console.log("running: " + running); 

if(isRunning[i] == "1") { 

var exph = 'Exph' + i; 
var expg = 'Exp' + i; 

exphour = document.getElementById(exph).innerHTML; //exphour 
currexp = document.getElementById(exp).innerHTML; //current gained exp 
exphour =parseInt(exphour); 
currexp =parseInt(currexp); 

console.log("currexp= " + currexp); 
console.log("exphour= " + exphour); 

expmin = exphour/60; 
console.log("expmin= " + expmin); 
expsec = Math.round(expmin/60); 
console.log("expsec= " + expsec); 

newtotalexp = currexp + expsec; 
console.log("newtotalexp= " + newtotalexp); 

if (document.getElementById) { document.getElementById(exp).innerHTML = newtotalexp; } //writing new exp 
else if (document.lexphouryers) { 
document.lexphouryers.theTime.document.write(time); 
document.lexphouryers.theTime.document.close(); } 

} else { 
//Do nothing 
return; 
} 
} 
timeout = setTimeout("experiencehour(1)", 1000); 
} 
} 

function variable1hour(var1) { 
if(var1 == 0) { //if there is dvar1hourtvar1hour 
console.log("var1=0 so no data is present, var1: " + var1); 

} else if(var1 == 1){ 

var isRunning = new Array(); 
isRunning[0] = " "; 
for (var i=0; i < 3; i++) { 
var bool = 'running' + i; 
console.log("Running = " + bool); 
isRunning[i] = document.getElementById(bool).innerHTML.substring(10,11); //checks if bot running 
console.log("isRunning = " + isRunning[i]); 



if(isRunning[i] == "1") { 

var varh = 'Varh' + i; 
var varg = 'Var' + i; 

console.log("Varh = " + varh); 
console.log("Var = " + varg); 

var1hour = document.getElementById(varh).innerHTML; //var1hour 
currvar1 = document.getElementById(varg).innerHTML; //current gained var1 
var1hour =parseInt(var1hour); 
currvar1 =parseInt(currvar1); 

console.log("currvar1= " + currvar1); 
console.log("var1hour= " + var1hour); 

var1min = var1hour/60; 
console.log("var1min= " + var1min); 
var1sec = Math.round(var1min/60); 
console.log("var1sec= " + var1sec); 

newtotalvar = currvar1 + var1sec; 
console.log("newtotalvar= " + newtotalvar); 

if (document.getElementById) { document.getElementById(varg).innerHTML = newtotalvar; } //writing new var1 
else if (document.lvar1houryers) { 
document.lvar1houryers.theTime.document.write(time); 
document.lvar1houryers.theTime.document.close(); 
} 

} else { 
//Do nothing 
return; 
} 
} 
timeout = setTimeout("variable1hour(1)", 1000); 
} 
} 

function stopScript() { 
console.log("Stopping script"); 
clearTimeout(timeout); 
} 

function startScript(i) { 
variable1hour(i); 
experiencehour(i); 
runningtime(i); 
} 

如何,我可以解決這個問題,所以我可以同時運行的所有3種功能的任何想法。

我已經檢查控制檯,並沒有得到任何錯誤,將阻止他們運行。

回答

0

你不能同時在JS中。但你可以模擬在JS中同時有點。

看看下劃線的defer方法。

http://underscorejs.org/#defer

它會像這樣工作:

  • 搭袢身體包起來在功能
  • 每次迭代通過for循環,使用作出的函數調用推遲

基本上就是這樣。這樣做會允許其他函數「中斷」任何給定的進程(函數),從而「共享」執行線程。

標準JS實現無延遲。我試圖模擬延期的更接近。這裏關鍵的一點是,雖然這些調用仍然按照它們排隊的順序執行,但在它們中的任何一個完成之前,所有調用都會「開始」。在AJAX異步請求的情況下,異步響應應該能夠在任何兩次循環迭代之間注入自身。您也可以通過輕微的延遲

http://jsfiddle.net/t2z9A/

for(var i = 0; i != 5; ++i) 
{ 
    (function(index) 
    { 
     document.getElementById('id' + index).innerHTML = 'started...'; 

     setTimeout(function() 
     { 
      // kill some time 
      var str = ''; 
      for(var j = 0; j != 10000000; ++j) 
       str = str + ' '; 

      document.getElementById('id' + index).innerHTML = 'Function: 1. Index: ' + index + ' - ' + new Date().getTime(); 
     }, index); 
    })(i); 
} 

for(var i = 5; i != 10; ++i) 
{ 
    (function(index) 
    { 
     document.getElementById('id' + index).innerHTML = 'started...'; 

     setTimeout(function() 
     { 
      // kill some time 
      var str = ''; 
      for(var j = 0; j != 1000000; ++j) 
       str = str + ' '; 

      document.getElementById('id' + index).innerHTML = 'Function: 2. Index: ' + index+ ' - ' + new Date().getTime(); 
     }, 1); 
    })(i); 
} 
+0

小記設置數據處理:你越「推遲」,您的循環體拼接成的單位,更多的共享它會。在執行另一個函數之前,每個函數都會執行直到完成。所以,切片和骰子儘可能多,以便獲得您想要的執行共享類型。 –

+0

如此理想,我想在for循環的每次迭代之後推遲,以便一次更新幾乎一行,然後向下移動? – Scott

+0

是的。例如:for(var i = 0; i!= 3; ++ i){_.defer(fn,i); }'其中'fn'是主體的函數,並且它需要執行的唯一參數是'i'。並且快速瀏覽你的代碼,它看起來就是你所需要的。試一試!我用一個標準的JS實現編輯我的代碼進行比較。模擬一個「中斷」是非常棘手的,但我想告訴你它是如何排隊的,而不是等待整個for循環(模擬函數)完成後才能繼續下一個循環。我希望這有幫助。 –