Jeremi Roivas
New Coder
Hi, I try creating 7 webworkers to simultanously fetch week data from remote server over internet. How ever I can get working 1 or 2 workers fine but if I try combine the result set to main() function with variable that listeners listen only 2 counters increse counter and main() will never get fired:
Console gets C counter added to 2 or 3 but then C says NAN on console and c never gets to 7 to fire main(). I suspect that localserver id too fast and simultanous data saving makes corruption happen.
JavaScript:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>i4ware - Timesheet for Jira</title>
<link type="text/css" rel="stylesheet" href="./extjs/resources/ext-theme-neptune/ext-theme-neptune-all.css" media="all">
<link type="text/css" rel="stylesheet" href="./custom.css" media="all">
<script type="text/javascript" src="./extjs/ext-all.js"></script>
<script type="text/javascript" src="./js/WebWorker.js"></script>
<script type="text/javascript" src="./js/custom.js"></script>
<script type="text/javascript">
var day1 = new Worker('worker.js');
var day2 = new Worker('worker.js');
var day3 = new Worker('worker.js');
var day4 = new Worker('worker.js');
var day5 = new Worker('worker.js');
var day6 = new Worker('worker.js');
var day7 = new Worker('worker.js');
var c = 0;
var a = '';
var b = '';
var c = '';
var d = '';
var e = '';
var f = '';
var g = '';
function tulosta() {
day1.postMessage('24/4/2022|Matti Kiviharju');
day2.postMessage('25/4/2022|Matti Kiviharju');
day3.postMessage('26/4/2022|Matti Kiviharju');
day4.postMessage('27/4/2022|Matti Kiviharju');
day5.postMessage('28/4/2022|Matti Kiviharju');
day6.postMessage('29/4/2022|Matti Kiviharju');
day7.postMessage('30/4/2022|Matti Kiviharju');
//console.log('Ollaan Main funtiossa');
//console.dir(tuloste);
//document.getElementById('tulostus').innerHTML = 'Vittu';
};
/*myWorker.onerror = function (e) {
console.log(e);
alert(e.message);
}*/
day1.onmessage = function (y) {
a = y.data;
console.log('Tulostetaan day1: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
day2.onmessage = function (y) {
b = y.data;
console.log('Tulostetaan day2: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
day3.onmessage = function (y) {
c = y.data;
console.log('Tulostetaan day3: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
day4.onmessage = function (y) {
d = y.data;
console.log('Tulostetaan day4: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
day5.onmessage = function (y) {
e = y.data;
console.log('Tulostetaan day5: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
day6.onmessage = function (y) {
f = y.data;
console.log('Tulostetaan day6: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
day7.onmessage = function (y) {
g = y.data;
console.log('Tulostetaan day7: ' + y.data);
c++;
console.log(c);
if (c == 7) {
main();
};
};
function main () {
if (c == 7) {
console.log('Tulostetaan ruudulle');
document.getElementById('tulostus').innerHTML = a + b + c + d + f + g;
};
};
</script>
</head>
<body>
<button id="alota" value="Start" onclick="tulosta()">
<div style="border: 1px black solid;width:800px; height: 600px;background-color:white;"><p id="tulostus"></p><div>
<body>
<html>
Console gets C counter added to 2 or 3 but then C says NAN on console and c never gets to 7 to fire main(). I suspect that localserver id too fast and simultanous data saving makes corruption happen.
Last edited: