473,461 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why does "if condition" executes in this below code-Javascript-requestAnimationFrame

12 Nibble
I'm currently watching tutorials to build projects as I'm still not in a phase where I can carve a project that I want all on my own.
Currently, working on a snake game.

Expand|Select|Wrap|Line Numbers
  1. let speed = 2;
  2. let lastPaintTime = 0;
  3.  
  4. //Game functions
  5. function main(ctime) {
  6.   window.requestAnimationFrame(main);
  7.   if ((ctime - lastPaintTime) / 1000 < 1 / speed) {
  8.     return;
  9.   }
  10.   lastPaintTime = ctime;
  11.   gameEngine();
  12. }
  13.  
  14. //Main logic starts here
  15. window.requestAnimationFrame(main);
  16.  
My confusion:
"If condition" should never be checked on this code. Because:
1) window.requestAnimationFrame(main): It calls main function.
2) At the very first line of main function, it again calls main function. So the control should go to main function and forever it should keep calling itself.
3) The if condition should never be checked.


But I asked chatGPT, and it says that if condition will be executed.

It says that it doesn't immediately call the main function but schedule/queue it.

What's this behavior called in Javascript language? Where can I read more about it. Is this common for every programming language?

Things I've read from chatGPT

requestAnimationFrame is a method that schedules a function to be called before the next repaint of the browser window. It does not immediately call the function, but rather adds it to a queue of functions to be called at a later time. This allows the browser to update the screen at a consistent frame rate, while also allowing other tasks to be performed in between frames.
Jan 7 '23 #1
0 1344

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: chotiwallah | last post by:
suppose i have the following code: switch ($inc): case 0: require 'inc0.php'; break; case 1: require 'inc1.php'; break; case 2:
3
by: Tjerk Wolterink | last post by:
Why o Why does a browser treat this xml file as an xhtml file. <!DOCTYPE xc:content > <xc:xcontent xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent"...
20
by: Scott Simons | last post by:
Why doesn't the compiler throw an error on a block of code like this: public string Email { get { return Email; } }
1
by: news.microsoft.com | last post by:
Hi, I've just finished developing an web site locally (ASP 1.1) which works fine. Now that I have posted it to our server (which has 1.0 and 1.1 installed) I get the following error message when...
9
by: Johnfli | last post by:
ADODB.Recordset error '800a0cb3' Current Recordset does not support bookmarks. This may be a limitation of the provider or of the selected cursortype. I am moving my webserver from NT4 using SQL...
0
by: Daniel | last post by:
What do FTP servers do when file send fails durring a file upload? Are partial files ever written? does ftp protocol cover this?
18
by: sunny | last post by:
Hi Why does C allows declaration of variable inside switch block. ex: foll prg does not gives "undeclared "b" error msg. but also does not initialize b to 20 int a=1; switch(a) { int b=20;...
0
by: Daniel | last post by:
how does ado.net SqlDataReader.GetString() know which encoding to read the data into a string as? Does sql sever set this at the column data type level, server wide encoding setting, os encoding?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.