Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1x 1x 4x 4x 4x 4x 8x 8x 8x 8x 4x 4x 4x 4x 7x 7x 7x 7x 4x 7x 7x 7x 7x 7x 77x 77x 39x 39x 39x 77x 7x 7x 4x 1x 7x 3x 1x 2x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x | /** * 用 requestAnimationFrame 包装定时器 * - 如果浏览器不支持 requestAnimationFrame API,则使用 BOM 原本的定时器API * @module fx/timer * @example * var $timer = require('@spore-ui/kit/packages/fx/timer'); * $timer.setTimeout(function () { * console.info('output this log after 1000ms'); * }, 1000); */ var Timer = {}; var noop = function () {}; function factory(methodName) { var wrappedMethod = null; Iif (typeof window === 'undefined') return; var win = window; // 如果有对应名称的方法,直接返回该方法,否则返回带有对应浏览器前缀的方法 var getPrefixMethod = function (name) { var upFirstName = name.charAt(0).toUpperCase() + name.substr(1); var method = win[name] || win['webkit' + upFirstName] || win['moz' + upFirstName] || win['o' + upFirstName] || win['ms' + upFirstName]; Eif (typeof method === 'function') { return method.bind(win); } return null; }; var localRequestAnimationFrame = getPrefixMethod('requestAnimationFrame'); var localCancelAnimationFrame = getPrefixMethod('cancelAnimationFrame') || noop; Eif (localRequestAnimationFrame) { var clearTimer = function (obj) { Eif (obj.requestId && typeof obj.step === 'function') { obj.step = noop; localCancelAnimationFrame(obj.requestId); obj.requestId = 0; } }; var setTimer = function (fn, delay, type) { var obj = {}; var time = Date.now(); delay = delay || 0; delay = Math.max(delay, 0); obj.step = function () { var now = Date.now(); if (now - time > delay) { fn(); Iif (type === 'timeout') { clearTimer(obj); } else { time = now; } } obj.requestId = localRequestAnimationFrame(obj.step); }; localRequestAnimationFrame(obj.step); return obj; }; if (methodName === 'setInterval') { wrappedMethod = function (fn, delay) { return setTimer(fn, delay, 'interval'); }; } else if (methodName === 'setTimeout') { wrappedMethod = function (fn, delay) { return setTimer(fn, delay, 'timeout'); }; } else if (methodName === 'clearTimeout') { wrappedMethod = clearTimer; } else Eif (methodName === 'clearInterval') { wrappedMethod = clearTimer; } } Iif (!wrappedMethod && this[methodName]) { wrappedMethod = this[methodName].bind(this); } return wrappedMethod; } /** * 设置重复调用定时器 * @method timer#setInterval * @memberof fx/timer * @param {Function} fn 定时重复调用的函数 * @param {Number} [delay=0] 重复调用的间隔时间(ms) * @returns {Object} 定时器对象,可传入 clearInterval 方法来终止这个定时器 */ Timer.setInterval = factory('setInterval'); /** * 清除重复调用定时器 * @method timer#clearInterval * @memberof fx/timer * @param {Object} obj 定时器对象 */ Timer.clearInterval = factory('clearInterval'); /** * 设置延时调用定时器 * @method timer#setTimeout * @memberof fx/timer * @param {Function} fn 延时调用的函数 * @param {Number} [delay=0] 延时调用的间隔时间(ms) * @returns {Object} 定时器对象,可传入 clearTimeout 方法来终止这个定时器 */ Timer.setTimeout = factory('setTimeout'); /** * 清除延时调用定时器 * @method timer#clearTimeout * @memberof fx/timer * @param {Object} obj 定时器对象 */ Timer.clearTimeout = factory('clearTimeout'); module.exports = Timer; |