I accidently made a post on StackOverflow, I'm new and wasn't intentional. I got redirected here. One person mentioned that the below test is not right because i'm using the scroll method and the way I was testing is invalid. Anyways if anyone can look at the variations of the code below and give me some pointers. I would really appreciate it. Thanks Rest of the Post below is copy paste.
I'm new so excuse me for any ignorance or misunderstanding on my part.
I decided to run a few test on js functions on jspref to see which method of using function is better suited for this small particular example. To get a better understanding.
LINK to the page with tested functions
Problem:
Getting different results each time, Cold Start, Re-test, Re-Re Test are all coming with different results
First Func - Is just embeded in normally
$(function() {
var up = $('#horiz_line').offset().top + $('#horiz_line').height() + 25,
triga = $('.trigger');
triga.css("display", "none");
$(window).scroll(function() {
var down = $(this).scrollTop();
(down > up) ? triga.fadeIn('fast') : triga.fadeOut('fast')
});
});
Second Func - Calling a Global Function
$(function() {
triga.css("display", "none");
$(window).scroll(function() {
scrollMenu($(this).scrollTop())
});
});
function scrollMenu(down) {
var up = $('#horiz_line').offset().top + $('#horiz_line').height() + 25,
triga = $('.trigger');
(down > up) ? triga.fadeIn('fast') : triga.fadeOut('fast')
}
Third Func - Registering/Calling variable func (To test/Learn: I have no idea wt i'm doing)
$(function() {
$(window).scroll(function() {
scrollMenu($(this).scrollTop())
});
var scrollMenu = function(down) {
var triga = $('.trigger'),
up = $('#horiz_line').offset().top + $('#horiz_line').height() + 25;
(down > up) ? triga.fadeIn('fast') : triga.fadeOut('fast')
}
});
Fourth Func - Same as Third except Up is called before hand, and registered as a global func. Which i'm guessing is not a good idea if the project gets bigger over time.
// Variable Up Takes up The Global Namespace. Is it better to pre-define it OR have it inside the scroll Menu anonymous func and get calculated each time the function is run?
$(function() {
var up = $('#horiz_line').offset().top + $('#horiz_line').height() + 25;
$(window).scroll(function() {
scrollMenu(up, $(this).scrollTop())
});
var scrollMenu = function(up, down) {
var triga = $('.trigger');
(down > up) ? triga.fadeIn('fast') : triga.fadeOut('fast')
}
});
What is a good practice and what not.
Thank you in advance.
TLDR:- I have no idea what I'm doing, just trying to learn a little.