简体   繁体   中英

How can I give data from my webworker to highchart

I am working with highcharts, I want to update highcharts series from my web-worker. My web-worker is receiving data from my api via xmlhttprequest now I want to update my chart without refreshing the web page , and how can I pass that data to highcharts series?

You need to use the onMessage event, and post messages from the worker to the client. Messages should be stringyfied object or arrays.

In your main code:

var worker = new Worker('w/main-worker.js');
worker.postMessage('message to worker');

worker.onmessage = function(e) {
    console.log(e.data);
    // do stuff with data from worker
};

In your worker:

// do stuff
// when stuff is ready
postMessage(myStuff);

// handle messages from the main thread
onmessage = function(e) {
    console.log(e.data);
    // do stuff with message from main thread
};

I have few demos here: https://github.com/mchaov/WebWorkers There are demos for similar use case like yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM