When you press the start button in view, the time is saved in localstorage and the timer function is called. Since the timer function is continuously called every second with setTimeout, console is displayed as 1,2,3… by console.log (elapsed) ;.
The value is calculated by dividing 31536000 (seconds of a year) from the elapsed seconds to get the elapsed years from that elapsed time.
I want to solve is press the start button and check the output of console.log (year);After pressing the start button, the first 3,6,9 and multiples of 3 are displayed. 1,2,2,2,3,3,3 ... and so on. I would like to solve the problem that when I press the start button, a strange value appears even though 0,0,0,0 must be displayed from the beginning.
timer.jsfunction timer () {
now = new Date ();
// Elapsed time (seconds)
elapsed = parseInt ((now.getTime ()-localStorage.getItem ("time"))/1000);
console.log (elapsed);
year = parseInt (elapsed/31536000);
console.log (year);
if (localStorage.getItem ("time")) {
var id = setTimeout ("repetition ()", 1000);
}
}
I thought that the numerical value (31536000) was too large to give an accurate value, so when dividing by 31536, it was calculated well and 0 has been output since the start button was pressed.
Also, instead of suddenly writing the year part in seconds, it is a formula for calculating the number of seconds in a year. If i rewrite it as year = parseInt (elapsed/365 * 24 * 60 * 60);1000 or more) was output irregularly.
I didn't know the cause at all. If i have a solution, please let me know.
-
Answer # 1
Related articles
- javascript - i want to receive and display a value in a dialog box
- javascript - unable to retrieve json format value with thymeleaf
- javascript - [chrome extension] i want to post the contents of the tab page, i want to pass a value to content_script
- i can't solve the javascript problem
- i want to apply reduce to the value of a javascript object
- javascript - i want to put a variable value in css
- javascript - how to increase the pull-down value
- # if you change the value once entered with the score input function with javascript, it will become nan
- javascript - i want to get the value associated with the array key in the object
- javascript - i want to display the value of the checkbox and replace it with another string depending on the conditions
- python - please tell me how to solve the value error that occurred when converting csv file data to numpy array
- javascript - when i display the value entered in the form, it is displayed as undefined
- javascript - get the value of the array in the object , how to use in an if statement
- i want to display the value obtained from postgresql with javascript on erb
- javascript - i want to display the value entered in the created form in slack with incoming webhooks
- javascript - i want to use a value initialized in one class in another unrelated class with no arguments
- javascript - i want to extract a value from a variable
- javascript - unable to emit value on socketio
- javascript - about the return value of async
- javascript - about the return value of queryselectorall ()
The cause of this bug is the use of
parseInt
even though the argument is numeric.parseInt takes a string as an argument.
Bugs can be solved by usingIn JavaScript, if the value is close to zero, such as
0.000000031709791983764586
, it is converted to a string with an exponential notation such as3.1709791983764586e-8
.So, for example,
parseInt (1/31536000)
becomesparseInt (3.1709791983764586e-8)
andparseInt ("3.1709791983764586e-8")
and finally3
.Math.floor
instead ofparseInt
.