# Trouble with a comparison

**URL:** <https://forum.kirupa.com/t/trouble-with-a-comparison/262908>\
**Category:** web dev\
**Created:** [June 11, 2008, 12:25am UTC](https://forum.kirupa.com/t/trouble-with-a-comparison/262908 "2008-06-11T00:25:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cold\_wolf](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@cold\_wolf](https://forum.kirupa.com/u/cold_wolf)\
**Post date:** [June 11, 2008, 12:25am UTC](https://forum.kirupa.com/t/trouble-with-a-comparison/262908/1 "2008-06-11T00:25:43Z")

</div>

I’m trying to detect when the user scrolls to the bottom of a page using jQuery, and this does it:

```auto
$(window).scroll(function () { 
  if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    fetchData();			
  }
});

```

But I want to detect when the window reaches 100px before the bottom of the page. So it would be the same as above, but with - 100 at the end like this:

```auto
if ($(window).scrollTop() == $(document).height() - $(window).height() - 100) {...}

```

Yet this doesn’t work, at all. I’ve tested the code elsewhere and it returns a valid value, so what gives? Anyone know?

---

<div class="post-metadata">

**Author:** ![cold\_wolf](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@cold\_wolf](https://forum.kirupa.com/u/cold_wolf)\
**Post date:** [June 13, 2008, 1:26pm UTC](https://forum.kirupa.com/t/trouble-with-a-comparison/262908/2 "2008-06-13T13:26:23Z")

</div>

Just thought I’d share my discovery. JavaScript events don’t fire very fast (JS in general is pretty ■■■■ slow) so when setting trigger points in pixels, _never_ use strict comparisons, ie only use “greater than” etc…

The problem here is fixed by simply replacing the strict equality “==” with “\>=”.

[rant]  
I keep finding that the hardest problems are the ones with the simplest solutions. Exercise in programming is exercise in mental flexibility! You have to be able to quickly step between the obvious and the subtle :sigh:  
[/rant]
