Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by rwniceing
at 2024-08-28 08:03:12
Point:500 Replies:6 POST_ID:829199USER_ID:12079
Topic:
JavaScript;;
I try to find out the running time for "for loop" , I guess probably it will be in milliseconds.
so I try to convert the new Date() into milli-second and subtracting it before and after the
loop code. but when I console.log(c-b); it will show wrong answer, why ?
How to define the b, c in double or long format for number and get the correct result of "c-b"
I expect it should be c-b=80 in milli seconds
Pleas advise
Rwniceing
Wrong console.log output from the following code
1- 1407193070584
2- 1407193070664
3- 1407166064013 //it is wrong I think
so I try to convert the new Date() into milli-second and subtracting it before and after the
loop code. but when I console.log(c-b); it will show wrong answer, why ?
How to define the b, c in double or long format for number and get the correct result of "c-b"
I expect it should be c-b=80 in milli seconds
Pleas advise
Rwniceing
Wrong console.log output from the following code
1- 1407193070584
2- 1407193070664
3- 1407166064013 //it is wrong I think
var i, a, b, c, max; max = 2e7; var d = Date.now(); var a = new Date();var y= a.getFullYear();var m=a.getMonth();var d=a.getDay();var b = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());console.log("1-",b);for (i = 0; i < max; i++) { a = 1234 + 5678 + i; b = 1234 * 5678 + i; c = 1234 / 2 + i;}var a = new Date();var y= a.getFullYear();var m=a.getMonth();var d=a.getDay();var c = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());console.log("2-",c);console.log("3-",c-b); 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:
Author: rwniceing replied at 2024-08-28 09:45:17
Sorry again, the topic post code is wrong since variable b
at var b = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());
is overwritten by b in for loop so that came out the wrong output
Conclude again topic post code with the mistake corrected by changed b to b2 for date that works , and .valueOf() also works if variable assignment is correct
this post is solved and closed.
at var b = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());
is overwritten by b in for loop so that came out the wrong output
Conclude again topic post code with the mistake corrected by changed b to b2 for date that works , and .valueOf() also works if variable assignment is correct
this post is solved and closed.
Expert: Ray Paseur replied at 2024-08-28 09:43:14
Might be good to post a new question here at E-E. This one is already answered and closed.
Author: rwniceing replied at 2024-08-28 09:29:24
When I change those Date variable from a to a2 ,b to b2 and c to c2 in previous post code. it will echo out the correct result on "c2-b2" without using .valueOf() function. I don;t know why ?
I have checked the previous code that is okay that there is no any variable conflict since a, b,c
variable in the for loop that doesn't affect the date() extraction.
Any expert could test those post code (previous on topic code and this post code as follows) to see what I say?
I have checked the previous code that is okay that there is no any variable conflict since a, b,c
variable in the for loop that doesn't affect the date() extraction.
Any expert could test those post code (previous on topic code and this post code as follows) to see what I say?
var i, a, b, c, max; max = 2e7; var d = Date.now(); var a2 = new Date();var y= a2.getFullYear();var m=a2.getMonth();var d=a2.getDay();var b2 = Date.UTC(y,m,d,a2.getHours(),a2.getMinutes(),a2.getSeconds(),a2.getMilliseconds());console.log("1-",b2);for (i = 0; i < max; i++) { a = 1234 + 5678 + i; b = 1234 * 5678 + i; c = 1234 / 2 + i;}var a2 = new Date();var y= a2.getFullYear();var m=a2.getMonth();var d=a2.getDay();var c2 = Date.UTC(y,m,d,a2.getHours(),a2.getMinutes(),a2.getSeconds(),a2.getMilliseconds());console.log("2-",c2);console.log("3-",c2-b2); 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:
Expert: Ray Paseur replied at 2024-08-28 09:18:53
This is kind of simplified, but it seemed to work OK. Only tested for very short intervals, and it probably would need at least the addition of minutes to achieve greater reliability. That said, using the confirm() and clicking it quickly I was able to get down to about 900ms.
http://iconoun.com/demo/temp_rwniceing.php
http://iconoun.com/demo/temp_rwniceing.php
<!DOCTYPE html><html dir="ltr" lang="en-US"><head><!-- SEE http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28507319.html --><meta charset="utf-8" /><meta name="robots" content="noindex, nofollow" /><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>HTML5 Page with Simple JavaScript Elapsed Time (ms) Calculation</title></head><body><script>var ad = new Date();var am = ad.getMilliseconds();var as = ad.getSeconds();var xx = confirm('Continue?');var zd = new Date();var zm = zd.getMilliseconds();var zs = zd.getSeconds();var az = (zs * 1000 + zm) - (as * 1000 + am);alert('This took ' + az + ' ms.');</script></body></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:
Author: rwniceing replied at 2024-08-28 08:25:27
Done with .valueOf() , thanks for your reply
Accepted Solution
Expert: COBOLdinosaur replied at 2024-08-28 08:21:55
500 points EXCELLENT
avascript does not do math very well.
you might be better to use the raw date value in micro second with a.valueOf(); and use those values for the calculation. Reference here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf
Cd&
you might be better to use the raw date value in micro second with a.valueOf(); and use those values for the calculation. Reference here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf
Cd&