ECMA6 OOP With Classes, Extend a New Feature

by sphere360 in Circuits > Software

458 Views, 1 Favorites, 0 Comments

ECMA6 OOP With Classes, Extend a New Feature

benchm.png

The next instructable is building on the last 2 and it's still the same app for which we add a new feature.

We will measure the time in H:M:S:MS, how long it takes till the calculation is finished.

diaedit.png

Here is the edited diagram with the new module "MeasureTime".

So the huge benefit of OOP is you can extend easily the desired feature.

New Directory, File

dirs.png

We create a new directory and file (bench/measuretime.js) in the projectfolder.

Content of the MeasureTime.js

timer.png

The measureTime.js is a minimalistic stopwatch that counts from 0 milliseconds, when it's started. It's synchronized with new Date().getTime() (Systemtime), so that the accuracy is by 1/1000sec.

Then it gets converted (modulo - remainder) returned as H,M,S,MS, when the watch is stopped.

ImportScripts

bench.png

The odd thing is, the worker has its own module loader (importScripts) and for the generic JS there is still no native module loader support. (using require.js)

postMessage has now 2 parameters summarized in an array.

By importing you can use the stoptimer with startBench and stopBench like showed in the pic.

Controller File

cont.png

We assign to the elapsed.innerHTML the returned time from the worker.

Testing

test.png

When we test the app, the elapsed time should appear.

The updated app is as zip available, happy coding/testing.