<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<style type="text/css">
#container {
padding: 0.5em;
}
#singleTask {
float: left;
width: 50%;
}
table {
border: 1px solid blue;
}
td {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Task Type Test</h1>
<div id="container">
<div id="singleTask">
<h2>Single task</h2>
<button id="createTableSingleTask">Create Table As Single Task</button>
</div>
<div id="multipleTask">
<h2>Multiple tasks</h2>
<button id="createTableMultipleTask">Create Table As Multiple Tasks</button>
</div>
</div>
<script type="text/javascript">
document.getElementById('createTableSingleTask').onclick = createTable;
document.getElementById('createTableMultipleTask').onclick = startCreateTable;
totalRows = 100;
totalCols = 100;
function createTable(evt) {
evt = evt || window.event;
var targ = evt.target || evt.srcElement;
var p = document.createElement('p');
var timeStart = new Date();
var timeStop;
removeContentAfter(targ);
targ = targ.parentNode;
appendTable(targ, totalRows, totalCols);
timeStop = new Date();
targ.appendChild(p);
p.appendChild(document.createTextNode(timeStop - timeStart + ' milliseconds'));
}
function startCreateTable(evt) {
evt = evt || window.event;
var targ = evt.target || evt.srcElement;
removeContentAfter(targ);
targ = targ.parentNode;
targ.p = document.createElement('p');
targ.timeStart = new Date();
processCreateTable(targ, totalRows, totalCols, function (el) {
return function () {
finishCreateTable(el);
}
}(targ));
}
function removeContentAfter(el) {
var parent = el.parentNode;
console.log('lastChild is' + parent.lastChild);
while (parent.lastChild !== el) {
console.log('removing ' + parent.lastChild);
parent.removeChild(parent.lastChild);
console.log('lastChild is' + parent.lastChild);
}
}
function finishCreateTable(el) {
el.timeStop = new Date();
el.appendChild(el.p);
el.p.appendChild(document.createTextNode(el.timeStop - el.timeStart + ' milliseconds'));
}
function appendTable(el, rows, cols) {
var table = document.createElement('table');
var tbody = document.createElement('tbody');
el.appendChild(table);
table.appendChild(tbody);
var i;
for (i = 0; i < rows; i += 1) {
tbody.appendChild(createTableRow(cols));
}
}
function createTableRow(cols) {
var tr = document.createElement('tr');
var i;
for (i = 0; i < cols; i += 1) {
td = document.createElement('td');
td.appendChild(document.createTextNode(' '));
tr.appendChild(td);
}
return tr;
}
function processCreateTable(el, rows, cols, callback) {
el.table = document.createElement('table');
el.appendChild(el.table);
el.tbody = document.createElement('tbody');
el.table.appendChild(el.tbody);
setTimeout(processRows(el, rows, cols, callback), 0);
}
function processRows(el, rows, cols, callback) {
return function () {
var tbody = el.tbody;
var i;
if (rows > 10) {
for (i = 0; i < 10; i += 1) {
tbody.appendChild(createTableRow(cols));
}
rows -= 10;
setTimeout(processRows(el, rows, cols, callback), 0);
} else {
for (i = 0; i < rows; i += 1) {
tbody.appendChild(createTableRow(cols));
}
callback();
}
};
}
</script>
</body>
</html>
Bookmarks