Showing
1 changed file
with
31 additions
and
3 deletions
@@ -10,13 +10,41 @@ | @@ -10,13 +10,41 @@ | ||
10 | const $ = require('yoho-jquery'); | 10 | const $ = require('yoho-jquery'); |
11 | const Overlay = require('./overlay'); | 11 | const Overlay = require('./overlay'); |
12 | const template = require('components/loading.hbs'); | 12 | const template = require('components/loading.hbs'); |
13 | -const _ = require('lodash'); | 13 | + |
14 | const AJAX_LOADING_ENABLED = false; // 全局控制 | 14 | const AJAX_LOADING_ENABLED = false; // 全局控制 |
15 | 15 | ||
16 | if (!Overlay) { | 16 | if (!Overlay) { |
17 | throw new Error('Required dependency "Overlay" not found!'); | 17 | throw new Error('Required dependency "Overlay" not found!'); |
18 | } | 18 | } |
19 | 19 | ||
20 | +/** | ||
21 | + * 创建一个无抖动的函数 | ||
22 | + * @param {[type]} func [description] | ||
23 | + * @param {[type]} wait [description] | ||
24 | + * @param {[type]} immediate [description] | ||
25 | + * @return {[type]} | ||
26 | + */ | ||
27 | +function debounce(func, wait, immediate) { | ||
28 | + let timeout; | ||
29 | + | ||
30 | + return function() { | ||
31 | + let context = this, args = arguments; | ||
32 | + let later = function() { | ||
33 | + timeout = null; | ||
34 | + if (!immediate) { | ||
35 | + func.apply(context, args); | ||
36 | + } | ||
37 | + }; | ||
38 | + let callNow = immediate && !timeout; | ||
39 | + | ||
40 | + clearTimeout(timeout); | ||
41 | + timeout = setTimeout(later, wait); | ||
42 | + if (callNow) { | ||
43 | + func.apply(context, args); | ||
44 | + } | ||
45 | + }; | ||
46 | +} | ||
47 | + | ||
20 | class Loading { | 48 | class Loading { |
21 | constructor(opts) { | 49 | constructor(opts) { |
22 | this.defaults = {}; | 50 | this.defaults = {}; |
@@ -63,13 +91,13 @@ const instance = new Loading(); | @@ -63,13 +91,13 @@ const instance = new Loading(); | ||
63 | 91 | ||
64 | if (AJAX_LOADING_ENABLED) { | 92 | if (AJAX_LOADING_ENABLED) { |
65 | $(document).ajaxStart(()=> { | 93 | $(document).ajaxStart(()=> { |
66 | - _.debounce(()=> { | 94 | + debounce(()=> { |
67 | instance.show(); | 95 | instance.show(); |
68 | }, 100)(); | 96 | }, 100)(); |
69 | }); | 97 | }); |
70 | 98 | ||
71 | $(document).ajaxStop(()=> { | 99 | $(document).ajaxStop(()=> { |
72 | - _.debounce(()=> { | 100 | + debounce(()=> { |
73 | instance.hide(); | 101 | instance.hide(); |
74 | }, 500)(); | 102 | }, 500)(); |
75 | }); | 103 | }); |
-
Please register or login to post a comment