Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
PHP / XMLHttpRequest POST HTML5 Worker Store json echo in variable

I see a lot of these questions comming by. But I seem to do something wrong. So forgive me for asking (I am a bit of a n00b). In my php I echo the the folowing echo json_encode(['msg'=>$NewTotal]);.

I use a HTML5 worker to post data to a php on my server and I need the echo to be posted back to the main script so I can repost it to set it as new value for the variable Totalaccounts for the loop.

Worker Script:

  onmessage = function(dbs) {
        console.log(dbs.data);
      var Totalaccounts = dbs.data;
      
    var DBScriptLoop = setInterval(
    (function () {
      DBStartWorking(); 
      //postMessage(Totalaccounts);  
    }), 123000);
    function DBStartWorking(){
    for (var dw = 0; dw < Totalaccounts; dw++) {
        setTimeout(function() {
    httpRequest = new XMLHttpRequest()
    var dataresponse = httpRequest.responseText;
    var NewCount = dataresponse.msg;
    httpRequest.open('POST', 'DostuffV1.php')
    httpRequest.send(Totalaccounts) 
    postMessage(NewCount);
    console.log(NewCount);
        }, 1200 * dw);
    }
    } // End script loop
    
    
    };

Main script:

var DBScriptWorker;

function startDBScriptWorker() {
  if(typeof(Worker) !== "undefined") {
    if(typeof(DBScriptWorker) == "undefined") {
 
     DBScriptWorker = new Worker('DBScriptWorker.js');
      var php_va = "<?php echo $AccToRank; ?>";
      DBScriptWorker.postMessage(php_va);
      
    }
    DBScriptWorker.onmessage = function(event) {
      document.getElementById("result").innerHTML = event.data;
      DBScriptWorker.postMessage(event.data);
    };
  } else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
  } 
}
function stopDBScriptWorker() { 
  DBScriptWorker.terminate();
  DBScriptWorker = undefined;
}

In my network tab I see the response is this: {"msg":29}

My variable is not set, it commes up as undifined. What am I doing wrong? Searching and trying for hours now..

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda