0

I'm trying to change my gaTop var in my functiontest but I don't understand why it doesn't work

var gaTop = 0;

functionTest = function(callback)
{
    $('html').on("mousewheel DOMMouseScroll", function(e)
    {
        var delta = (e.originalEvent.wheelDelta || -e.originalEvent.detail);

        if (delta < 0)
        {
            var gaTop = -100;
        }
        else if (delta > 0)
        {
            var gaTop = +100;
        }

        console.log(gaTop);

        callback(gaTop);
    });
}

functionTest(function(e)
{
    console.log(gaTop);
});
3
  • 2
    your declaring and initializing gaTop in every block and you are not using ga. Commented Dec 27, 2016 at 14:22
  • You have to make gaTop as a global variable or declare it in such a scope where both function can access it Commented Dec 27, 2016 at 14:24
  • i finally find the solution : Commented Dec 27, 2016 at 14:54

3 Answers 3

1

As reference: Declaring vs Initializing a variable?

var ga = 0;
var gaTop = null;
functionTest = function(callback) {
$('html').on("mousewheel DOMMouseScroll", function(e) {
  var delta = (e.originalEvent.wheelDelta || -e.originalEvent.detail);
  if (delta < 0) {
    gaTop = -100;
  } 
  else if (delta > 0) {
    gaTop = 100;
  }
  console.log(gaTop);
  callback(gaTop);

});
}
functionTest(function(e) {
 console.log(gaTop);
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

i'm sorry i did a mistake, my var ga should be gaTop. But the console.log(gaTop) always return 0
0

Initialize the variable at top before assign the value. otherwise it won't set, you are creating new variable and set the value in both if and else part that is the cause of issue.

var gaTop = null;
 if (delta < 0) {
     gaTop = -100;
  } 
  else if (delta > 0) {
    gaTop = +100;
  }

2 Comments

i'm sorry i did a mistake, my var ga should be gaTop. But the console.log(gaTop) always return 0
debug and verify the delta value in on mousewheel event
0

its finally working, thanks for your help !

var gaTop = 0;

            functionTest = function(callback) {
            $('html').on("mousewheel DOMMouseScroll", function(e) {
              var delta = (e.originalEvent.wheelDelta || -e.originalEvent.detail);
              if (delta < 0) {
                gaTop = gaTop -100;
              } 
              else if (delta > 0) {
                gaTop = gaTop +100;
              }
              callback(gaTop);

            });
            }
            functionTest(function(e) {
             console.log(gaTop);
            });     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.