lab-brussels-1 / home

Home repository for Lab Brussels 1.
https://lab-brussels-1.github.io/home
MIT License
4 stars 5 forks source link

Maria: Asynchronous Programming, 3 weeks #387

Open MMikhailova opened 2 years ago

MMikhailova commented 2 years ago

Asynchronous Programming

Learning Objectives

MMikhailova commented 2 years ago

Week 1

I pushed my progress in recreating Stopwatch.net here: My Stopwatch

I Need Help With:

When I was recreating Stopwatvh.net, I faced an issue related to counting milliseconds with

  setInterval ( function()=>{}, 1)`.

It doesn't count properly rather too slow. After long searching for a solution in Google, I came across the only way to count ms correctly link.

var startTime = Date.now();
var interval = setInterval(function() {
    var elapsedTime = Date.now() - startTime;
}, 100);

Can you recommend other solution as to milliseconds are counted correctly?

Also how can I extract ms from 0 to 999 from a number: E.g elapsedTime=76102 (ms) ms = 102// ? sec = 16 //This I know min = 1 //know

I did it by msCorrect=(ms / 1000).toFixed(3).toString().split('.')[1] but I'm sure there is an easier way.

What went well?

I understand the JavaScript Event Loop, and can demonstrate this by using setTimeout and setInterval to schedule simple tasks. I found two solution for the timer: first is by counting seconds and then using if loop to get mins and hrs. However, it doesn't work correctly for counting milliseconds and second is the one described above.

What went less well?

It was hard for me to find a solution for counting milliseconds, so I spent some time. I haven't done exercises yet.

Lessons Learned

I am going to finish exercises by Sunday.

danielhalasz commented 2 years ago

Can you recommend other solution as to milliseconds are counted correctly?

here you can read about a slightly more accurate option with performance.now(): https://michaelscodingspot.com/measure-execution-time-in-javascript/

danielhalasz commented 2 years ago

Also how can I extract ms from 0 to 999 from a number: E.g elapsedTime=76102 (ms) ms = 102// ? sec = 16 //This I know min = 1 //know

I did it by msCorrect=(ms / 1000).toFixed(3).toString().split('.')[1] but I'm sure there is an easier way.

sorry but not quite sure what you are trying to achieve here.. πŸ™ convert ms to ms?

MMikhailova commented 2 years ago

In given example https://stopwatch.net/ the milliseconds are displayed in '000Β΄ format. It means that after 999 counter starts again from 0. The method that i used counts ms but don't restart after 999. I tried to use 'if loop' but it doesn't work because can't capture the moment correctly. So if I have 1001 ms how can I display 1ms?

I used: performaceSensitiveFunc() { const start = Date.now(); // ... const duration = Date.now() - start; // when timer on 1001 how to show 001 ms? }

danielhalasz commented 2 years ago

check how @samirm00 has done it... by checking if ms is 1000, then adding 1 second to the state and resetting ms to 0: https://github.com/samirm00/stop-watch/blob/main/src/logic/time.js

MMikhailova commented 2 years ago

Week 2

I pushed my progress here: User-AP

I Need Help With:

What is the main difference in using Promises vs Async functions, besides Async function is easier to read.

Can I get any information from a URL by using fetch function or there are limits?

What went well?

I was able to accomplish User task given by Samir and applied changes he suggested. I also helped my classmates and together we have achieved their goals.

What went less well?

It was a hectic week. I had to travel and had a little time to study. Hope to catch up next week.

Lessons Learned

How to get a specific user from a data base by fetching. Using preventDefault() method to prevent an event from happening by default.