javascript - why global variable is not changed? -
global variables not change values. pagenumber remains 1 , supposed incremented in function. try implement continious scroll , next block of code, using post query action in asp.net mvc controller, of course gets me same result
<script type="text/javascript"> pagenumber = 1; //infinite scroll starts second block nomoredata = false; inprogress = false; function scrollfunction(pagenumber, nomoredata, inprogress) { var documentheight = $(document).height(); var windowheight = $(window).height(); var windowscrolltop = $(window).scrolltop(); var documentminuswindowheight = documentheight - windowheight; //-1 because when scroll reaches bottom of document windowscrolltop - documentheightminuswindowheight equals 0 1 if ((windowscrolltop > documentminuswindowheight - 1) && !nomoredata && !inprogress) { inprogress = true; $("#loadingdiv").show(); $.post("@url.action("infinitescroll", "products")", { "productid": '@model.productviewmodel.id', "pagenumber": pagenumber }, function (data) { pagenumber++; nomoredata = data.nomoredata; $("#productdealspartialdiv").append(data.htmlstring); $("#loadingdiv").hide(); inprogress = false; }); } } $(document).ready(function () { //var pagenumber = 1; //var nomoredata = false; //var inprogress = false; scrollfunction(pagenumber, nomoredata, inprogress); $(window).scroll(function () { scrollfunction(pagenumber, nomoredata, inprogress); }); }); </script>
but code snippet works absolutely correct.
<script type="text/javascript"> $(document).ready(function () { var pagenumber = 1; //infinite scroll starts second block var nomoredata = false; var inprogress = false; $(window).scroll(function () { var documentheight = $(document).height(); var windowheight = $(window).height(); var windowscrolltop = $(window).scrolltop(); var documentminuswindowheight = documentheight - windowheight; //-1 because when scroll reaches bottom of document windowscrolltop - documentheightminuswindowheight equals 0 1 if ((windowscrolltop > documentminuswindowheight - 1) && !nomoredata && !inprogress) { inprogress = true; $("#loadingdiv").show(); $.post("@url.action("infinitescroll", "products")", { "productid": '@model.productviewmodel.id', "pagenumber": pagenumber }, function (data) { pagenumber++; nomoredata = data.nomoredata; $("#productdealspartialdiv").append(data.htmlstring); $("#loadingdiv").hide(); inprogress = false; }); } }); }); </script>
that's because first parameter of function scrollfunction
called pagenumber
.
so when call it, function manipulate local variable called pagenumber
not global one. raw types such integers or strings not passed reference value, means changing local pagenumber
not affect global one.
look @ these examples:
function change(glob) { glob = 2; } var glob = 1; change(glob);
after call, glob still equals 1, happening in code. indeed decided call argument "glob" anything:
function change(localparameter) { localparameter = 2; } var glob = 1; change(glob);
on example assume understand calling change
should not affect global variable. exact same happening above if variable names same.
now avoid , work directly on global variable, have using variable function , not passing parameter:
function change() { glob = 2; } var glob = 1; change();
now glob equals 2 because function had direct access variable above scope. call closing variable.
Comments
Post a Comment