function get(url, callback) {
const request = new XMLHttpRequest();
request.addEventListener('load', function (event) {
try {
const data = JSON.parse(request.response);
callback(data);
} catch (error) {
callback(request.response);
}
});
request.open('GET', url);
request.send();
}
get('https://jsonplaceholder.typicode.com/todos', function (data) {
console.log(data);
});
get('URL_1', function (data_1) {
get('URL_2', function (data_2) {
get('URL_3', function (data_3) {
// do something
});
});
});
function get(url) {
return new Promise(function (resolve, reject) {
const request = new XMLHttpRequest();
request.addEventListener('load', function (event) {
try {
const data = JSON.parse(request.response);
resolve(data);
} catch (error) {
resolve(request.response);
}
});
request.addEventListener('error', function (event) {
reject({
status: request.status,
text: request.statusText
});
});
request.open('GET', url);
request.send();
});
}
Promise.all([
get('URL_1'),
get('URL_2'),
get('URL_3')
]).then(function ([data_1, data_2, data_3]) {
// do something
});
callback
-функций в их promise
вариант. pre
вывести.fetch('https://jsonplaceholder.typicode.com/todos')
.then(response => response.json())
.then(data => {
const element = document.querySelector('pre');
element.textContent = JSON.stringify(data, null, '\t');
});
for (let i = 0; i < tasks.length; i++) {
...
li.innerHTML = tasks[i];
...
}
for (let task of tasks) {
...
li.innerHTML = task;
...
}
build
специально для продакшена сделано, а serve
для разработки?package.json
, чтобы --watch
работал, если очень надо. class EventEmitter {
constructor() {
this.handlers = new Map();
}
on(event, handler) {
const handlers = this.handlers.has(event) ? this.handlers.get(event) : new Set();
handlers.add(handler);
this.handlers.set(event, handlers);
}
emit(event, ...args) {
if (this.handlers.has(event)) {
const handlers = this.handlers.get(event);
for (let handler of handlers) {
handler(...args);
}
}
}
}
on
у CustomSelecton(event, callback) {
this.events.on(event, callback);
}
closeDropdown
дописываем this.events.emit('close');
а для customSelectInstanse
customSelectInstanse.on('close', () => {
console.log('Close');
});
assignValue
creditTermRange.value = creditTermRange.value;
меняем на creditTerm.value = creditTermRange.value;
totalAmountOfCredit.innerHtml= `${lounAmount} ₽`;
totalMonthlyPayment.innerHtml= `${monthlyPaymentArounded} ₽`;
totalRecommendedIncome.innerHtml= `${monthlyPaymentArounded + ((monthlyPaymentArounded / 100) * 35)} ₽`
.innerHTML
, но я советую использовать .textContent
function verifyLogin(login) {
return new Promise(function (resolve, reject) {
connection.query(`SELECT login FROM user_data WHERE login = '${login}'`, function (error, results, fields) {
if (error) {
reject(error);
}
if (results[0] !== undefined) {
resolve('Логин уже используется');
} else {
resolve(0);
}
});
});
}
methods: {
playVid: function() {
this.playing = !0,
this.$refs.vid.play()
},
initializeMorph: function() {
var t = this
, e = 0;
this.morphInterval = setInterval((function() {
var a = t.$refs.morph;
!function(t, e, a) {
var r = ["a", "b", "c", "d", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "{", "}", "%", "$", "?", "!"]
, n = 15
, i = e.split("")
, s = a.split("")
, o = i.length
, l = s.length
, c = new Date
, d = c.getTime()
, u = 0
, h = 0
, p = 105 / Math.max(o, l);
!function e() {
c = new Date,
h += c.getTime() - d;
for (var a = u; a < Math.max(o, l); a++) {
var f = Math.floor(Math.random() * (r.length - 1));
i[a] = r[f]
}
if (h >= p) {
u += Math.floor(h / p);
for (var g = 0; g < u; g++)
i[g] = s[g] || null;
h = 0
}
t.textContent = i.join(""),
d = c.getTime(),
u < Math.max(o, l) && setTimeout((function() {
window.requestAnimationFrame(e)
}
), 1e3 / n)
}()
}(a, a.textContent, t.words[e]),
e < t.words.length - 1 ? e++ : e = 0
}
), 3e3)
}
},
beforeDestroy: function() {
clearInterval(this.morphInterval),
clearInterval(this.safariCheck)
}
event
и сбрасываем его.menuToggle.addEventListener('click', function (event) {
event.preventDefault();
...
});
this
меняем на menuToggle
.<button class="menu-toggle" id="menu-toggle">
<span class="menu-toggle__span"></span>
</button>
.menu-toggle {
width: 60px;
height: 60px;
padding: 0;
border: none;
display: flex;
justify-content: center;
align-items: center;
background: none;
z-index: 15;
}
.menu-toggle.active .menu-toggle__span {
background-color: transparent;
}
.menu-toggle.active .menu-toggle__span:before {
transform: rotate(45deg);
top: 0;
}
.menu-toggle.active .menu-toggle__span:after {
transform: rotate(-45deg);
top: 0;
}
checkbox
'а внутри «кнопки». array.reduce((accumulator, value, index) => {
if (!(accumulator[index % 2] instanceof Array)) {
accumulator[index % 2] = [];
}
accumulator[index % 2].push(value);
return accumulator;
}, []);
array.reduce((accumulator, value, index) => {
accumulator[index % 2].push(value);
return accumulator;
}, [[], []]);
array.reduce((accumulator, value, index) => (accumulator[index % 2].push(value), accumulator), [[], []]);
array.reduce((a, v, i) => (a[i % 2].push(v), a), [[], []]);
const countOff = (array, count) => array.reduce((accumulator, value, index) => {
accumulator[index % count].push(value);
return accumulator;
}, Array.from({ length: count }, () => []));
countOff([1, 2, 3, 4, 5, 6], 2); // [[1, 3, 5], [2, 4, 6]]
countOff([1, 2, 3, 4, 5, 6], 3); // [[1, 4], [2, 5], [3, 6]]
Math.pow
уже не работает?const pow = (number, power) => {
if (number === 1 || power === 0) {
return 1;
}
const count = Math.round(Math.abs(power));
let result = number;
for (let index = 0; index < count - 1; index++) {
result *= number;
}
if (power < 0) {
return 1 / result;
} else {
return result;
}
};
console.log(Math.pow(2, 2) === pow(2, 2)); // true
console.log(Math.pow(2, -2) === pow(2, -2)); // true
console.log(Math.pow(2, -3) === pow(2, -3)); // true
console.log(Math.pow(3, -4) === pow(3, -4)); // true
function mynumber(a, b) {
var c = 1;
if (b < 0) {
for (var i = b; i < 0; i++) {
c /= a;
}
} else {
for (var i = 1; i <= b; i++) {
c *= a;
}
}
return c;
}