Yes, I know that this is probably a very stupid question, but it has been undermining me for some time.
Okay, that's why Iβve been studying JavaScript for some time now and understood perfectly.,. except for the functions "parameters" (I think they are called).
I was taught that they work like this:
function test(number) { document.write(number); }; test(1); test(12);
That makes sense to me. However, recently I came across some that were a little different.
var counterDays = document.getElementById('days'); var counterHours = document.getElementById('hours'); var counterMinutes = document.getElementById('minutes'); var counterSeconds = document.getElementById('seconds'); var date = new Date('December 28, 2016 00:00:00'); function updateTimer(date) { var time = date - new Date(); return { 'days': Math.floor(time / (1000 * 60 * 60 * 24)), 'hours': Math.floor((time/(1000 * 60 * 60)) % 24), 'minutes': Math.floor((time / 1000 / 60) % 60), 'seconds': Math.floor((time / 1000) % 60), 'total': time }; }; function startTimer(counterDays, counterHours, counterMinutes, counterSeconds, date) { var timerInterval = setInterval(function() { var timer = updateTimer(date);
<span id="days"> </span> <span id="hours"> </span> <span id="minutes"> </span> <span id="seconds"> </span>
I seriously don't understand why updateTimer always needs date in parentheses when the date variable is an existing variable in the global scope. Same thing with startTimer . I do not understand why I need to convey this. Why not just access the variable inside the function, since they have a global scope, and you need to do this. Instead, I need to pass the variable as a parameter for the function to work.
I tried and tried, but the only way to get this function to work is to pass variables. Why is this?
As I was still learning, I searched the Internet for more information about functions and their parameters, but everyone shows me something similar to my first example. I know that all this probably just turned on my head, but for life I just do not understand.
Note. I'm still involved, so sorry if this whole question is just plain stupid.
Also, the JS code I'm having a problem with doesn't actually start. This is because I donβt want to embed all my code, but rather from the code I'm having problems with.
javascript html
Bzcc
source share