All files / fx flashAction.js

100% Statements 17/17
70% Branches 7/10
100% Functions 3/3
100% Lines 16/16

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                                                  1x     1x               1x 1x 5x     1x 5x 5x 1x 1x   4x 2x 2x   2x 2x           1x  
/**
 * 封装闪烁动作
 * @method fx/flashAction
 * @param {object} options 选项
 * @param {number} [options.times=3] 闪烁次数,默认3次
 * @param {number} [options.delay=100] 闪烁间隔时间(ms)
 * @param {function} [options.actionOdd] 奇数回调
 * @param {function} [options.actionEven] 偶数回调
 * @param {function} [options.recover] 状态恢复回调
 * @example
 * var $flashAction = require('@spore-ui/kit/packages/fx/flashAction');
 * // 文字闪烁,奇数次呈现为红色,偶数次成纤维蓝色,动画结束呈现为黑色
 * var text = $('#target span.txt');
 * $flashAction({
 *   actionOdd : function (){
 *     text.css('color', '#f00');
 *   },
 *   actionEven : function (){
 *     text.css('color', '#00f');
 *   },
 *   recover : function (){
 *     text.css('color', '#000');
 *   }
 * });
 */
var $assign = require('../obj/assign');
 
function flashAction(options) {
  var conf = $assign({
    times: 3,
    delay: 100,
    actionOdd: null,
    actionEven: null,
    recover: null,
  }, options);
 
  var queue = [];
  for (var i = 0; i < conf.times * 2 + 1; i += 1) {
    queue.push((i + 1) * conf.delay);
  }
 
  queue.forEach(function (time, index) {
    setTimeout(function () {
      if (index >= queue.length - 1) {
        Eif (typeof conf.recover === 'function') {
          conf.recover();
        }
      } else if (index % 2 === 0) {
        Eif (typeof conf.actionEven === 'function') {
          conf.actionEven();
        }
      } else Eif (typeof conf.actionOdd === 'function') {
        conf.actionOdd();
      }
    }, time);
  });
}
 
module.exports = flashAction;