XStack

Deep into details

0%

1
2
3
4
// 在没有jQuery的页面上快速插入jQuery
var s = document.createElement("script");
s.src = "http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js";
document.body.appendChild(s);

代码#

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
var throttle = function(func, wait, options) {
var getTime = function(){return new Date().getTime()};
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : getTime();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = getTime();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
var debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = new Date().getTime() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};

return function() {
context = this;
args = arguments;
timestamp = new Date().getTime();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};

说明#

  • throttle相当于不停执行的动作会间歇性发出, 适用于滚动或拖动场景
  • debounce相当于在动作停止之后一定时间才执行动作, 适用于键盘停止后一定时间执行动作的场景, 如搜索

使用工具:
MacVim + amix/vimrc
(不再探讨功能属于MacVim还是vim配置文件)

  1. 快速移动多行文本:
    v选中后, command+j控制上下移动
  2. 按照当前路径打开新文件(而不是每次从home下再去查找):
    :set autochdir
    (设置的地址在~/.vim_runtime/my_configs.vim)
  3. 操作命令:
  • caw - 替换单词
  • ci’ - 替换单引号内所有字符