旧的$(窗口).scroll函数突然不起作用 – 向上移动两个变量来纠正它?

我已经使用下面的function已经有一段时间了,并且它一直在顺利工作并且有意。 然而,当我今天回去检查网站的状态时,我意识到它不再工作了。 使用console.log似乎变量thresholdCrossedthresholdCrossedTop在它们之前的函数之前被更新,即使它们在函数上下文中被放置在它之下。 怎么会这样?

将前面提到的两个变量向上移动到该函数的末尾(否则应该在更新变量之前执行)解决了这个问题。

这是旧版本:

 jQuery.fn.followTo = function () { var $this = this; jQuery(window).scroll(function(e){ var scroll = jQuery(window).scrollTop(); jQuery(function (){ if(!thresholdCrossedTop && scroll > containerOffsetTop && scroll  pos) { // custom function here } else if (thresholdCrossed && scroll <= pos) { // custom function here } else if(thresholdCrossedTop && scroll  pos; // Was updating too soon thresholdCrossedTop = scroll > containerOffsetTop; // Was updating too soon }); }; jQuery('#indhold-inner').followTo(indholdHeight); 

这是固定版本:

 jQuery.fn.followTo = function () { var $this = this; jQuery(window).scroll(function(e){ var scroll = jQuery(window).scrollTop(); jQuery(function (){ if(!thresholdCrossedTop && scroll > containerOffsetTop && scroll  pos) { // custom function here } else if (thresholdCrossed && scroll <= pos) { // custom function here } else if(thresholdCrossedTop && scroll  pos; // moved up thresholdCrossedTop = scroll > containerOffsetTop; // moved up }); }); }; jQuery('#indhold-inner').followTo(indholdHeight); 

最初我想知道它是否与Chrome或其他东西的更新有关,导致脚本运行异步(我不知道..),但我也在IE 11中测试过,同样的行为在这里显而易见。