tip.js 1006 Bytes
/**
 *  tip
 *  @author chenxuan <xuan.chen@yoho.cn>
 *
 *  @example
 *  tip('lalalalal~')
 *
 * @example
 *  tip({
 *     txt: 'lalalalal',
 *     delay: 2000
 *  })
 */

import $ from 'jquery';

function tip(param, cb) {
    let timer;
    const viewData = {
        mask: false,
        delay: 2000,
        txt: ''
    };

    if (typeof param === 'string') {
        Object.assign(viewData, {
            txt: param
        });
    } else {
        Object.assign(viewData, param);
    }

    const tipView = `<div class="tip-box"><div class="tip">${viewData.txt}</div></div>`;
    const $oldTip = $('.tip-box');

    if ($oldTip.length) {
        timer = $oldTip.data('timer');
        clearTimeout(timer);
        $oldTip.remove();
    }

    const $tip = $(tipView).appendTo(document.body);

    $tip.data('timer', setTimeout(function() {
        $tip.remove();
        if (typeof cb === 'function') {
            cb();//eslint-disable-line
        }
    }, viewData.delay));
}

export default tip;