Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!