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 Answers3

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);
}); 
Community
  • 1
  • 1
serkan
  • 6,885
  • 4
  • 41
  • 49
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;
  }
Balaji Marimuthu
  • 1,940
  • 13
  • 13
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);
            });