From 568d9acd8552c3b65e67a7a2a83d184682e1d18a Mon Sep 17 00:00:00 2001 From: Steshin Denis Date: Sun, 29 May 2022 15:23:57 +0300 Subject: [PATCH 1/2] Task is done --- src/ex15_js-async/task-01.js | 26 ++++++++++++++++++++ src/ex15_js-async/task-02.html | 14 +++++++++++ src/ex15_js-async/task-02.js | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/ex15_js-async/task-01.js create mode 100644 src/ex15_js-async/task-02.html create mode 100644 src/ex15_js-async/task-02.js diff --git a/src/ex15_js-async/task-01.js b/src/ex15_js-async/task-01.js new file mode 100644 index 0000000000..4373f3c547 --- /dev/null +++ b/src/ex15_js-async/task-01.js @@ -0,0 +1,26 @@ +// const requestURL = 'https://jsonplaceholder.typicode.com/users/'; + +function someFetch(method, url, body = null) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open(method, url); + xhr.responseType = 'json'; + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.onload = () => { + if (xhr.status === 200 || xhr.status === 201) { + resolve(xhr.response); + } + reject(xhr.response); + }; + xhr.onerror = () => { + reject(xhr.response); + }; + if (body === null) { + xhr.send(); + } else { + xhr.send(JSON.stringify(body)); + } + }); +} + +module.exports = someFetch; diff --git a/src/ex15_js-async/task-02.html b/src/ex15_js-async/task-02.html new file mode 100644 index 0000000000..9fef32a2e5 --- /dev/null +++ b/src/ex15_js-async/task-02.html @@ -0,0 +1,14 @@ + + + + + + + Task 02 + + + + + + + diff --git a/src/ex15_js-async/task-02.js b/src/ex15_js-async/task-02.js new file mode 100644 index 0000000000..fb4c9da3fa --- /dev/null +++ b/src/ex15_js-async/task-02.js @@ -0,0 +1,43 @@ +const search = document.getElementById('search'); +const text = document.getElementById('text'); + +const time = 500; +const mockArray = [ + '123', + '234', + '312', + 'red', + 'rad', + 'black', + 'blur', + 'green', + 'glass', +]; + +console.log(mockArray); + +function searchItem(...rest) { + const searchValue = search.value; + mockArray.forEach((item) => { + if (item.includes(searchValue) && item[0] === searchValue[0]) { + text.value = item; + } + }); + if (!searchValue) { + text.value = null; + } + console.log(rest); +} + +function debounce(callback) { + let isRunning; + + return (...rest) => { + clearTimeout(isRunning); + isRunning = setTimeout(() => { + callback.apply(this, ...rest); + }, time); + }; +} + +search.addEventListener('input', debounce(searchItem)); From 054b8e8c489d3df8881dbb16b9dbd5f4be182990 Mon Sep 17 00:00:00 2001 From: Steshin Denis Date: Sun, 29 May 2022 16:49:57 +0300 Subject: [PATCH 2/2] task is done --- src/ex15_js-async/task-01.js | 2 -- src/ex15_js-async/task-02.html | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ex15_js-async/task-01.js b/src/ex15_js-async/task-01.js index 4373f3c547..8f9773c83e 100644 --- a/src/ex15_js-async/task-01.js +++ b/src/ex15_js-async/task-01.js @@ -1,5 +1,3 @@ -// const requestURL = 'https://jsonplaceholder.typicode.com/users/'; - function someFetch(method, url, body = null) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); diff --git a/src/ex15_js-async/task-02.html b/src/ex15_js-async/task-02.html index 9fef32a2e5..e34cff64ff 100644 --- a/src/ex15_js-async/task-02.html +++ b/src/ex15_js-async/task-02.html @@ -4,7 +4,7 @@ - Task 02 + Task 2