-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataService.js
76 lines (58 loc) · 2.46 KB
/
dataService.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
'use strict';
// sorry about spaggeti code and so unoptimized =(
// it was too late to fix this
(function () {
let url = 'https://api.meetup.com/2/open_events?key=676e713e5f123c3f786f2277597a7a43&' +
'photo-host=public' +
'&zip=98101&' +
'topic=web,machine-learning, softwaredev,computer-programming,web-development&' +
'category=34&' +
'time=' + getNewWeekInMs() + ', ' + getEndOfNewWeekInMs(getNewWeekInMs())+ '&' +
'only=name,description,time,venue';
window.CallbackRegistry = {};
function scriptRequest(url, onSuccess, onError) {
let scriptOk = false;
let callbackName = 'cb' + String(Math.random()).slice(-6);
url += ~url.indexOf('?') ? '&' : '?';
url += 'callback=CallbackRegistry.' + callbackName;
CallbackRegistry[callbackName] = function(data) {
scriptOk = true;
delete CallbackRegistry[callbackName];
onSuccess(data);
};
function checkCallback() {
if (scriptOk) return;
delete CallbackRegistry[callbackName];
onError(url);
}
let script = document.createElement('script');
// в старых IE поддерживается только событие, а не onload/onerror
script.onreadystatechange = function() {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
this.onreadystatechange = null;
setTimeout(checkCallback, 0);
}
}
// события script.onload/onerror срабатывают всегда после выполнения скрипта
script.onload = script.onerror = checkCallback;
script.src = url;
document.body.appendChild(script);
}
function ok(data) {
console.log("Strat: " + getNewWeekInMs());
console.log("Stop: " + getEndOfNewWeekInMs(getNewWeekInMs()));
mainFlow(data.results, getNewWeekInMs(), getEndOfNewWeekInMs(getNewWeekInMs()));
}
function fail(url) {
alert( 'Ошибка при запросе ' + url );
}
function getNewWeekInMs() {
let today = new Date();
let newWeek = new Date(today.getTime() + (7 - today.getDay() + 2) * 24 * 60 * 60 * 1000);
return newWeek.setUTCHours(0, 0, 0, 0);
}
function getEndOfNewWeekInMs(newWeek) {
return newWeek + 7 * 24 * 60 * 60 * 1000 - 1000;
}
scriptRequest(url, ok, fail);
})();