Don't understand this argument

Code:

<html>
<head>
	<script>
	function blink() {
		var blink_speed = 1000; // every 1000 == 1 second, adjust to suit
		var t = setInterval(function () {
		var ele = document.getElementById('Foo');
		ele.style.visibility = (ele.style.visibility == 'hidden' ? '' : 'hidden');
		}, blink_speed);
	}
</script>
</head>
<body onload="blink();">
	<?php
	$trkg_guest_incentive_sum = 14.92;
	$str = "You'll save up to an addional <span id=\\"Foo\\" >$" . number_format($trkg_guest_incentive_sum,2) . "</span> on these items as a abc.com member. </br> So join today!";
	echo '<h5 style="text-align:center;margin:7px 0px 0px 0px;color:blue;"> ' . $str . '</h5>'; 
	//<p style""> blah blah blah <span id="Foo" style"">Blink</span> blah blah blah </p>
	?>

</body>
</html>

I found something like this on stackoverflow before I found the blank tag.

I don’t understand how blink_speed qualifies as an argument. How’s that possible given function()?

The second argument of setInterval() is whatever value is set to blink_speed, so this is going to alternate (eternally) the SPAN tag contents between hidden and visible every second (1000 ms = 1 sec).

OK. So, please confirm that function() is the first argument.

Also is there a term for for using function() as an argument? I’ve been calling it an unnamed function.

Yes, the function() is the first argument for setInterval() (the code to run) which is repeated as often as the second argument is set for in milliseconds.

And AFAIK, I believe that “unnamed function” or “anonymous function” is correct.

Thanks. I’m following you.

How does the second argument do that? I don’t see how it gets passed to blink() as milliseconds.

It doesn’t get passed to blink() - it’s being SET inside the blink function, called from the body “onload” attribute. You could pass it to blink, then pass that on as the second argument, if you wanted.

When it’s spread out across many lines, it can be a bit difficult to read. However, if you put it all on one line:

blink_speed = 1000;

var t = setInterval(function () { var ele = document.getElementById(‘Foo’); ele.style.visibility = (ele.style.visibility == ‘hidden’ ? ‘’ : ‘hidden’); }, blink_speed);

Red is the variable that is being set and the beginning/end of setInterval().
Green is the first argument, the function.
Purple is the second argument, the time in milliseconds that the function will be triggered.

Understood.

How does the function know its in ms and not hamburgers?

setInterval takes two parameters.

The first is the function you want to have run at regular intervals.

The second is a time interval in milliseconds.

It knows that the time is in milliseconds because that is what the setInterval function expects the second parameter to be.

If you try to pass an invalid parameter to the function then the function will attempt to convert what you pass to what it expects. So if you passed a string as the first parameter it would eval() the string in an attempt to convert it to a function. If you passed hamburgers as the second parameter it would attempt to convert that to milliseconds.

That’s what the standards committee decided the second argument would be. :slight_smile:

Thanks WolfShade and felgall.

I didn’t recognize setInterval() as a method.