什么鬼算法

var testarr = [] 
var prevScroll = 0
var cache = 0
// 仅在区域内计算
    if (y >= testarr[0] && y <= testarr[testarr.length - 1]) {
      // down
      if (y >= prevScroll) {
        var down = (function (x) {
          // 如果存在缓存索引,并且满足滚动的区域,那么return
          if (cache != null && y >= testarr[cache] && y < testarr[cache + 1]) return false

          // 如果入参是null,说明是第一次,初始化为0吧
          x = x == null ? 0 : x;

          // 判断当前滚动是否在某个范围内
          if (y >= testarr[x] && y < testarr[x + 1]) {
            // 这里是你要写的代码
            console.log('down', 'ABCDEFGHJKLMNPQRSTWXYZ'.split('')[x])
            // 设置缓存
            cache = x
            // 保险起见,虽然绝对不会溢出。如果没有的话,那么增加索引, 继续往下搜索
          } else if (x < testarr.length - 1) {
            // 递归
            down(++x);
          }
        });

        down(cache);

        // up
        // TODO: 如果小于第一个的话,应该取消
        // TODO: 如果是从中间开始的情况要考虑
        // TODO: 避免重复执行
      } else {
        var up = (function (x) {
          if (y < testarr[x]) {
            console.log('up', 'ABCDEFGHJKLMNPQRSTWXYZ'.split('')[x - 1])
            cache = x - 1
          }
        });

        up(cache);
      }
    } else if (cache != null) {
      // 这里重置
      console.log(1)
      cache = null
    }

原文地址:https://www.cnblogs.com/CyLee/p/8757365.html