-
Notifications
You must be signed in to change notification settings - Fork 0
/
Misc.js
126 lines (104 loc) · 2.89 KB
/
Misc.js
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
"use strict"
function getCacher(func) {
var cache = {};
return function(x) {
if (!(x in cache)) {
cache[x] = func.call(this, x);
}
return cache[x];
}
}
function getLogger(func, log) {
return function() {
log.push([].slice.call(arguments));
return func.apply(this, arguments);
}
}
function applyAll(func) {
if (arguments.length == 1) {
return func();
} else {
arguments.shift = [].shift;
arguments.shift();
var args = [].slice.call(arguments);
return func.apply(null, args)
}
}
var NOW = 'только что'
var AGO = ' назад'
var MIN = ' мин.'
var SEC = ' сек.'
function formatDate(date) {
var diff, now, result
now = new Date()
diff = now.getTime() - date.getTime()
if (diff <= 1000) {
return NOW
}
if (diff < 60 * 1000) {
result = Math.round(diff/1000)
return result + SEC + AGO
}
if (diff < 60 * 60 * 1000) {
result = Math.round(diff/60000)
return result + MIN + AGO
}
return absFormatDate(date)
}
function absFormatDate(date) {
var hh = date.getHours();
if (hh < 10) hh = '0' + hh;
var mimi = date.getMinutes();
if (mimi < 10) mimi = '0' + mimi;
var dd = date.getDate();
if (dd < 10) dd = '0' + dd;
var mm = date.getMonth() + 1;
if (mm < 10) mm = '0' + mm;
var yy = date.getFullYear() % 100;
if (yy < 10) yy = '0' + yy;
return dd + '.' + mm + '.' + yy + ' ' + hh + ':' + mimi;
}
function getClass(obj) {
return {}.toString.call(obj).slice(8, -1);
}
function delay(func, ms) {
return function() {
var savedThis=this
var savedArgs=arguments
setTimeout(
function() {
return func.apply(savedThis, savedArgs)
}, ms)
}
}
function debounce(func, ms){
var debounced=false
return function(){
if (debounced == false){
debounced=true
setTimeout(function(){debounced=false}, ms)
return func.apply(this,arguments)
}
}
}
function throttle(func, ms) {
var isThrottled = false
var savedArgs, savedThis
function wrapper() {
if (isThrottled) {
savedArgs = arguments
savedThis = this
return
}
isThrottled = true
setTimeout(function() {
isThrottled = false
if (savedThis) {
wrapper.apply(savedThis, savedArgs)
savedArgs = savedThis = null
}
}, ms)
return func.apply(this, arguments);
}
return wrapper
}