0

我目前有一个由 MySQL 表填充的表,反过来我想从这些单元格中获取数据并使用它们来创建模拟的实时更新,但是由于我使用 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 个功能。

我检查了控制台,我没有收到任何会阻止它们运行的​​错误。

4

1 回答 1

0

你不能同时在 JS 中。但是你可以在 JS 中同时模拟一点。

看看下划线的defer方法。

http://underscorejs.org/#defer

它会这样工作:

  • 获取循环体并将其包装在一个函数中
  • 通过 for 循环的每次迭代,使用 defer 调用函数

基本上就是这样。这样做将允许其他函数“中断”任何给定的进程(函数),从而“共享”执行线程。

没有延迟的标准 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);
}
于 2012-11-09T03:50:28.780 回答