class Some {
constructor() {
this.data = null;
Promise.all([this.init()]).then(() => {
this.start();
});
}
start() {
console.log('START', this.data);
}
async init() {
const response = await fetch('https://jsonplaceholder.typicode.com/todos');
this.data = await response.json();
}
}
new Some();
loginig
у Вас ничего не принимает от сервера, как Вы хотите узнать о завершении операции? const loadScript = (url, callback) => {
const script = document.createElement('script');
script.addEventListener('load', () => {
callback();
});
script.src = url;
document.body.append(script);
};
loadScript('SOME_URL', () => {
console.log('Скрипт загружен');
});
const loadScript = url => new Promise(resolve => {
const script = document.createElement('script');
script.addEventListener('load', () => {
resolve();
});
script.src = url;
document.body.append(script);
});
loadScript('SOME_URL').then(() => {
console.log('Скрипт загружен');
});
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
const text = walker.currentNode.textContent.trim();
if (text === '000-00-00') {
walker.currentNode.textContent = '111-11-11';
}
}
const parse = float => parseFloat(float.replace(/,/g, ''));
const number = parse(objectFloat.param1) + parse(objectFloat.param2); // 2353.66
console.log(number.toLocaleString('en-US')); // 2,353.66
let inputValue = document.querySelector('input');
let btn = document.getElementsByTagName('button');
let ulListPlan = document.getElementById('plan');
let clearComleted = document.getElementById('completed');
clearComleted.addEventListener('click', function () {
let completed = document.querySelectorAll('input[type="checkbox"]:checked');
for (let entry of completed) {
entry.parentNode.remove();
}
});
function createTodo(value) {
let li = document.createElement('li')
let checkBox = document.createElement('input');
let close = document.createElement('button');
let text = document.createTextNode(value);
close.textContent = "X";
checkBox.type = "checkBox";
checkBox.addEventListener('input', function () {
if (checkBox.checked) {
li.style.userSelect = 'none';
li.style.backgroundColor = 'grey'
} else {
li.style.backgroundColor = 'white';
li.style.userSelect = "text";
}
});
close.addEventListener('click', function () {
li.remove();
});
li.append(checkBox, text, close);
ulListPlan.append(li);
}
inputValue.addEventListener('keyup', function (event) {
if (event.keyCode == 13 && inputValue.value.length > 0) {
createTodo(inputValue.value);
inputValue.value = '';
}
});
createLi.onclick
дописываете createLi.onclick = null;
doneList.ondblclick = function () {
doneList.removeChild(createLi);
}
меняете на createLi.ondblclick = function () {
createLi.remove();
}
.onclick
и прочее надо менять на .addEventListener
. const sum = array => array.reduce((accumulator, value) => (accumulator += value), 0);
const breakUp = (number, count) => {
const minimal = (count * (count + 1)) / 2;
if (minimal > number) {
throw new Error(`Число ${number} слишком маленькое. Минимальное допустимое число ${minimal}.`);
}
const numbers = Array.from({ length: count }, (_, index) => (count - index));
numbers.forEach((value, index, array) => {
const total = sum(array);
const remainder = number - total;
if (total < number) {
const random = (index === count - 1)
? remainder + value
: Math.floor(Math.random() * remainder);
array[index] = random;
}
});
const isUnique = new Set(numbers).size === numbers.length;
if (!isUnique) {
return breakUp(number, count);
} else {
return numbers.sort((a, b) => (b - a));
}
};
const breakUp = (number, count) => {
const numbers = [];
for (let index = 0; index < count; index++) {
const random = index === count - 1
? number
: Math.round(Math.random() * number);
numbers.push(random);
number -= random;
}
numbers.sort((a, b) => b - a);
return numbers;
};
const numbers = breakUp(100, 4);
console.log(numbers); // [76, 15, 6, 3]
const breakUp = (number, count) => Array.from({ length: count }, (_, index) => {
const random = index === count - 1
? number
: Math.round(Math.random() * number);
number -= random;
return random;
}).sort((a, b) => b - a);
const breakUp = (number, count) => {
const numbers = new Set();
while (numbers.size < count) {
const previousSize = numbers.size;
const random = numbers.size === count - 1
? number
: Math.floor(Math.random() * number);
numbers.add(random);
if (previousSize < numbers.size) {
number -= random;
}
}
return [...numbers.values()].sort((a, b) => b - a);
};
const fetchData = () => {
fetch('...')
.then(response => response.json())
.then(({ group }) => {
this.setState({
data: group,
car: Object.keys(group)[0]
}, this.filter);
})
.catch(error => {
console.log('404 Not Found');
});
};
return false;
const images = [
{
selector: '#img',
dark: 'https://dummyimage.com/600x400/fc0/fff',
light: 'https://dummyimage.com/600x400/0f3/fff'
},
...
];
function swapImages() {
const isDark = $('.light').hasClass('dark');
for (let image of images) {
$(image.selector).attr('src', isDark ? image.dark : image.light);
}
}
$('.checkbox').click(function () {
$('.light').toggleClass('dark');
swapImages();
});
$(document).ready
дописываем swapImages();
const data = {
name: 'John',
gender: 'male'
};
const searchParams = new URLSearchParams();
for (let key in data) {
searchParams.append(key, data[key]);
}
location.search = searchParams;
location.search
, можно модифицировать параметры и в конце уже присвоить их заново. if (innerWidth >= 1280) {
if ($(window).scrollTop() > 150) {
$('.hide').fadeIn(900);
} else {
$('.hide').fadeOut(700);
}
}
if ($(window).scrollTop() > 150 && innerWidth > 1024) {
$('.hide').fadeIn(900);
} else {
$('.hide').fadeOut(700);
}