Skip to content

Commit 9c2873e

Browse files
authored
sample: runProductionLine
1 parent c00c7d1 commit 9c2873e

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Common use cases in production setting:
6565
> You might schedule maintenance task to be executed every 1000 hours of machine operation.\
6666
> By using ``setTimeout``, you can schedule maintenance task to be executed after specified delay, ensuring that task is executed at appropriate time.
6767
68-
#### There are two ways of running something regularly:
68+
There are two ways of running something regularly:
6969
+ ``setInterval(callback, delay)`` is method that allows you to schedule macrotask to be executed repeatedly at specified interval.
7070
> ```javascript
7171
> // repeat with the interval of 2 seconds
@@ -83,10 +83,40 @@ Common use cases in production setting:
8383
> setTimeout(() => { clearTimeout (timerId); console.log('stop'); }, 7000);
8484
> ```
8585
86-
87-
88-
89-
86+
> For example: we have a production line with 10 machines.\
87+
> Every 10 seconds, we want to calculate speed of each machine and update a dashboard with results.
88+
> > ```javascript
89+
> > function calculateMachineSpeed(machineId) {
90+
> > // perform some calculations to determine speed of machine
91+
> > const speed = Math.random() * 100;
92+
> > // update the dashboard with the machine's speed
93+
> > updateDashboard(machineId, speed);
94+
> > };
95+
> >
96+
> > function updateDashboard(machineId, speed) {
97+
> > // update the dashboard to display the machine's speed
98+
> > console.log(`Machine ${machineId} speed: ${speed.toFixed(2)}`);
99+
> > };
100+
> >
101+
> > function runProductionLine() {
102+
> > // assume we have 10 machines on production line
103+
> > const machineIds = Array.from({ length: 10 }, (_, i) => i + 1);
104+
> > const productionLineInterval = setInterval(() => {
105+
> > machineIds.forEach(machineId => {
106+
> > // calculate speed of each machine every 10 seconds
107+
> > setTimeout(() => {calculateMachineSpeed(machineId);}, 0);
108+
> > });
109+
> > }, 10000);
110+
> > // stop the execution of the program after 30 seconds
111+
> > setTimeout(() => {
112+
> > clearInterval(productionLineInterval);
113+
> > console.log('Production line has been shut down.');
114+
> > }, 30000);
115+
> > }
116+
> >
117+
> > // start the production line
118+
> > runProductionLine();
119+
> > ```
90120
91121
- - -
92122

0 commit comments

Comments
 (0)