So in previous questions I’ve been told to call/execute/start functions like thisFunc; instead of thisFunc();.
And I’ve found that sometimes that works and sometimes it doesn’t.
<script type='text/javascript'>
var valgo = 0;
var thing = "";
var lastPost = document.getElementById(<? echo $_SESSION['countything']; ?>);
lastPost.style.opacity = valgo;
function valgogoer(thing){
valgo += .05;
if (lastPost.style.opacity < 1){
lastPost.style.opacity = valgo;
}
}
setInterval(function(){valgogoer(<? echo $_SESSION['countything']; ?>)}, 50);
// Somethings are leftover from when I was messing with it, like the parameter thing.
</script>
In this code (and please tell me if it’s awful), because I’m using setInterval to call a function with a parameter, I found through research it must be called the way it is above.
So two questions
-
When am I actually supposed to use () in calling functions?
-
In the code above, how can I make it so that it stops executing that function after the opacity hits 1. Currently it’s restrained to 1, but it’s still being called, and I’ve got a feeling it’s better to stop the function being called, than to have it being called but not doing anything.
Thanks!
You use the brackets when you want to call the function. But if just wanna pass the content of the function, you do not. Examples:
In event handlers, you do not use the brackets because you have to say to the browser to execute the content of the function. If you put them, the browser will call the return value of the function, which may cause an error:
This same thing happens when you call the setInterval method with a function as the parameter. That’s why the brackets are so important