• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

can we write ( 0 < 5000 )

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we write   . can we write instead of   if not, then why?
 
Rancher
Posts: 4741
50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because in the first one ( while (new Date().getTime() - start < 5000);) it loops until timenow (new Date().getTime()) minus start is 5000 or more.
In your example (while (0 < 5000);) it will loop forever.
 
syed zeyad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Because in the first one ( while (new Date().getTime() - start < 5000);) it loops until timenow (new Date().getTime()) minus start is 5000 or more.
In your example (while (0 < 5000);) it will loop forever.



sir can you tell me how the following code works?

let start = newDate().getTime();
while (new Date().getTime() - start < 5000);

Thanks
 
syed zeyad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

syed zeyad wrote:

Dave Tolls wrote:Because in the first one ( while (new Date().getTime() - start < 5000);) it loops until timenow (new Date().getTime()) minus start is 5000 or more.
In your example (while (0 < 5000);) it will loop forever.



sir can you tell me how the following code works?

let start = newDate().getTime();
while (new Date().getTime() - start < 5000);

Thanks



and what is the meaning of following:

let start = newDate().getTime();
while (new Date().getTime() - start < 5000);
 
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't quote so much from previous posts; it is unnecessary.

syed zeyad wrote:. . .
and what is the meaning of following:

let start = newDate().getTime();
while (new Date().getTime() - start < 5000);
. . .

Have you declared newDate() elsewhere, or have you unintentionally run two words together?
What do you think it means? Have you looked up what the different parts of those two lines mean? What does while (...); with a semicolon at the end do?

And . . . welcome to the Ranch
 
syed zeyad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quoteDear Brother, i don't understand the meaning of this code,

could you help me in understanding this?
the code seems that it is subtracting and getting the result to 0 which is less than 5000
 
Dave Tolls
Rancher
Posts: 4741
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 1, start is defined as the current time in milliseconds (I expect).
That's what:

is doing.
(note I fixed your space issue).

This bit in the while loop:

is getting current time in milliseconds and subtracting the value of start.
This is going to change over time as the loop executes.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I thought was happening. I have, unfortunately, another view of what that code does. It causes the computer to run full speed unnecessarily for 5″ creating new Date objects by the million, and is a very inefficient way of making execution appear to do nothing very much for 5″.
In Java®, you could write something likeI don't know whether it is possible to write anything similar in JS>
 
Dave Tolls
Rancher
Posts: 4741
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes.
I intended to imply the same thing in my first post (I think).

It's a sleep.

And it's the wrong way to do it because, as you say, it spins the processor.

Javascript has a setTimeout method that is intended to "do something after a pause".
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . in my first post (I think).

That was obvious to me, but OP seems to have missed it.

. . . setTimeout method . . . .

That looks nicer to use than Thread#sleep()
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic