首页 > zepto的touch模块中,为什么tap事件要通过settimeout(fn,0)触发?

zepto的touch模块中,为什么tap事件要通过settimeout(fn,0)触发?

最近看touch.js源码。发现

tapTimeout = setTimeout(function() {
              // trigger universal 'tap' with the option to cancelTouch()
              // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
              //直接触发tap事件。
              var event = $.Event('tap')
              event.cancelTouch = cancelAll
              //清空所有定时器以及 清空touch对象。
              touch.el.trigger(event)
              // trigger double tap immediately  立即触发双击事件。
              if (touch.isDoubleTap) {
                if (touch.el) touch.el.trigger('doubleTap')
                touch = {}
              }

              // trigger single tap after 250ms of inactivity
              else {
                touchTimeout = setTimeout(function(){
                  touchTimeout = null
                  if (touch.el) touch.el.trigger('singleTap')
                  touch = {}
                }, 250)
              }
            }, 0)
          }

想知道,tapTimeout存在的意义,是在主干进程中,提出此回调函数,方便其他条件判断是否取消此定时器?


保证后执行。类似于then的作用~


主要是因为tap这个事件不是浏览器原生支持的,浏览器原生支持的事件touchstart,touchmove,touchend根据这几个事件可以封装出很多事件,类似tap,tap触发的时间晚于touchstart,早于click,但是何时是tap事件触发的时机就是一个问题了,采用延时的方法,保证tap事件不被多次执行

【热门文章】
【热门文章】