私は素数を見つけるための解決策に取り組んでおり、1 つの問題を除いてすべて問題ありません。Web ワーカー ファイルの i == 373 && j == 15 で停止します。HTML ページが消え、ページをリロードした後に実行を再開できます。しかし、入力パラメーターは同じで (localStorage に保存されています)、それは私を夢中にさせます。しばらくすると、実行が再び停止し、html 要素が消えます。また、コンソールのjsも機能しません。エラーは処理されません。途方に暮れています。ありがとうございました。
メイン.JS
var db = openDatabase("primeNumbers", "0.1", "Prime numbers db", 200000);
db.transaction(function (tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS primeNumbers (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, value INTEGER NOT NULL UNIQUE)", [], null, null);
});
var settings = {};
prepareData();
$(document).ready( function() {
$('#start_button').click( function() {
toggleButton();
});
$('#show_table_button').click(function() {
showTableButton();
});
$('#show_canvas_image_button').click(function() {
showCanvas();
});
});
//check if db can be used
if(!db){alert("Failed to connect to database.");}
var currNumber = 2;
var work;
var worker = new Worker('js/worker.js');//(blobURL);
//worker event listener
worker.onmessage = function(event) {
(event.data != undefined) && (settings.bFlag = true);
finder(true);
};
function finder(again) {
if(settings.j == 14 && settings.i == 373) {
console.log('70');
};
if(again) {
if ( settings.bFlag ){
settings.j = 0;
if ( settings.iShag == 3 ){
settings.i+=4;
settings.iShag = 0;
} else {
settings.i+=2;
settings.iShag++;
};
iSquare = Math.floor(Math.sqrt(settings.i));
settings.bFlag = false;
};
settings.j++;
};
storeTempData();
work && worker.postMessage('{\"i\":\"'+settings.i+'\",\"j\":\"'+settings.j+'\"}');
};
function toggleButton() {
btn = $('#start_button');
if(btn.text() != 'pause_calculations') {
btn.text('pause_calculations');
work = true;
finder(false);
} else {
btn.text('resume_calculations');
work = false;
};
};
// table with data
function showTableButton() {
//TODO: check if we can use this as parameter to function
btn = $('#show_table_button');
if(btn.text() == 'show_calculated_data') {
btn.text('reload_table');
renderTable();
} else {
//TODO: implement it renderTable();
};
};
var table_data;
function renderTable() {
getBlockOfDataFromDb( function() {
$('#table_with_results').jqGrid({
datatype: 'local',
data: table_data,
height: 230,
width: 350,
colNames: ['id', 'value'],
colModel: [
{ name: 'id', index: 'id', width: 20, sorttype: 'int', align: 'right' },
{ name: 'value', index: 'value', width: 20, sorttype: 'int', align: 'right'}
],
multiselect: false,
caption: 'Prime numbers from db',
rowNum: 10,
pager: 'table_pager',
pagination: true,
gridview: true,
edit:false,
add:false,
del:false
});
});
};
function getBlockOfDataFromDb(callback) {
var res = [];
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM primeNumbers', [], function(tx, result) {
result_length = result.rows.length;
for(var i = 0; i < result_length; i++) {
var json_obj = { id: result.rows.item(i).id, value: result.rows.item(i).value };
res.push(json_obj);
};
table_data = res;
callback();
});
});
};
Array.prototype.contains = function(k) {
for(var p in this)
if(this[p] === k)
return true;
return false;
};
function showCanvas() {
var primeArray = [];
var edge;
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM primeNumbers', [], function(tx, result) {
result_length = result.rows.length;
for(var i = 0; i < result_length; i++) {
primeArray.push(parseInt(result.rows.item(i).value));
};
edge = parseInt(Math.sqrt(Math.max.apply(Math, primeArray))) + 1;
canv_div = $('#canvas_div');
$('<canvas width="'+edge+'" height="'+edge+'" id="canvas_elem"></canvas>').appendTo(canv_div);
canv = $('#canvas_elem')[0];
var context = canv.getContext('2d');
var imgd = context.createImageData(edge,edge);
var pix = imgd.data;
var iter = 0;
for (var i = 0; n = pix.length, i < n; i += 4) {
pix[i] = 0;
if(primeArray.contains(iter)) {
pix[i+3] = 255;
} else {
pix[i+3] = 50;
};
iter++;
}
context.putImageData(imgd, 0,0);
});
});
};
function prepareData() {
var ls = localStorage;
if(ls.getItem('bFlag') && ls.getItem('i') && ls.getItem('j') && ls.getItem('iShag')) {
settings.bFlag = (ls.getItem('bFlag') === 'true');
settings.i = parseInt(ls.getItem('i'));
settings.j = parseInt(ls.getItem('j'));
settings.iShag = parseInt(ls.getItem('iShag'));
} else {
settings.i = 7;
settings.j = 1;
settings.iShag = 0;
settings.bFlag = false;
db.transaction(function(tx) {
tx.executeSql('INSERT INTO primeNumbers (value) SELECT 2 AS value UNION SELECT 3 UNION SELECT 5');
});
};
};
function storeTempData() {
localStorage.setItem('i' , settings.i);
localStorage.setItem('j' , settings.j);
localStorage.setItem('bFlag' , settings.bFlag);
localStorage.setItem('iShag' , settings.iShag);
};
そしてまた私のWORKER.JS
onmessage = function(event) {
data = JSON.parse(event.data);
i = parseInt(data['i']);
j = parseInt(data['j']);
iSquare = Math.floor(Math.sqrt(i));
run();
};
function run() {
var db = openDatabase("primeNumbers", "0.1", "Prime numbers db", 200000);
db.transaction(function(tx) {
if(i == 373 && j == 15) {
1+1;
};
tx.executeSql('SELECT value FROM primeNumbers WHERE id='+(j+1), [], function(tx, result) {
val = parseInt(result.rows.item(0)['value']);
if ( i % val == 0 ){
self.postMessage('bFlag');
} else if (j >= iSquare) {
db.transaction(function(tx) {
tx.executeSql('INSERT INTO primeNumbers (value) VALUES ('+i+')', [], function() {
self.postMessage('bFlag');
});
});
} else {
self.postMessage();
};
},
function(tx, error) {
self.postMessage('Error processing SQL: ' + error);
});
}, function(error) {
self.postMessage('Error processing Transaction: ' + error)
});
};
私はどんなアイデアでも幸せになります。おそらくsmbは、エラーをキャッチする方法や、クロムプロファイラーで何を見つける必要があるかを知っています. その過程を動画にしました。ユーチューブ