From 190076b3b682135e381186f701bdd28f554270cc Mon Sep 17 00:00:00 2001 From: Gregor Wuest Date: Sat, 30 May 2020 13:23:19 +0200 Subject: [PATCH] async await sleep --- css/notes_app.css | 6 +---- html/index.html | 3 --- scripts/notes_app.js | 56 +++++++++++++++++++++++++++----------------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/css/notes_app.css b/css/notes_app.css index 0870f95..b11df53 100644 --- a/css/notes_app.css +++ b/css/notes_app.css @@ -22,10 +22,6 @@ body{ border:1px black solid; } -#saveProgressDiv{ - display: none; -} - -#saveProgress{ +progress { width: 312px; } diff --git a/html/index.html b/html/index.html index 94c100d..6e11624 100644 --- a/html/index.html +++ b/html/index.html @@ -22,8 +22,5 @@
-
- -
\ No newline at end of file diff --git a/scripts/notes_app.js b/scripts/notes_app.js index a9764ad..0ecb8d7 100644 --- a/scripts/notes_app.js +++ b/scripts/notes_app.js @@ -7,21 +7,21 @@ $(document).ready(function(){ $('#allNotesDiv').on('click', 'button', function() { console.log("1") console.log(this) - //$("#saveProgressDiv").toggle(); - //saveNoteObject(this); + let buttonId = (this.id).split("saveNoteButton")[1]; + console.log(buttonId) + createProgressBar(buttonId); + saveNoteObject(buttonId); }) }) -function randomId(){ - return '_' + Math.random().toString(36).substr(2,9); - }; +let id=0; function createForm(){ - let id = randomId() + id++; let wrapper = document.createElement("div") + wrapper.setAttribute('id', id) let inputsWrapper = document.createElement("div") - //let form = document.createElement("form"); inputsWrapper.setAttribute('class', "noteDiv"); let input = document.createElement("input"); @@ -58,13 +58,22 @@ function createForm(){ let ctr; -function saveNoteObject(a){ - console.log(a) - //ctr = setInterval(progressBar, 1000); //simulate delay for saving note +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } - let noteTitle = document.getElementById('inputTitle').value; - let noteText = document.getElementById('noteText').value; +async function saveNoteObject(id){ + + console.log(id) + await sleep(10000) //sleep simulates the remote server access or similar, + //so instead of sleep would be e.g. saveToRemoteServer()... + //the leftover code of the async function is executed after the delay + + + let noteTitle = document.getElementById('inputTitle'+id).value; + let noteText = document.getElementById('noteText'+id).value; let noteObj = { + idK: id, title: noteTitle, text: noteText }; @@ -74,20 +83,25 @@ function saveNoteObject(a){ console.log(noteObj_serialized) } -function progressBar(){ - document.getElementById('saveProgress').value = - document.getElementById('saveProgress').value + 5; - if (document.getElementById('saveProgress').value == 100){ +function enableProgressBar(id){ + console.log('pBar'+id) + document.getElementById('pBar'+id).value = + document.getElementById('pBar'+id).value + 5; + if (document.getElementById('pBar'+id).value == 100){ clearInterval(ctr); } } +function createProgressBar(id){ - - - -function getNoteObject(){ - + let progressBar = document.createElement('progress'); + let progressBarWrapper = document.createElement('div'); + progressBarWrapper.setAttribute('class', 'pBars') + progressBar.setAttribute('value', '0'); + progressBar.setAttribute('max', '100'); + progressBar.setAttribute('id', 'pBar'+id); + progressBarWrapper.appendChild(progressBar); + document.getElementById(id).appendChild(progressBarWrapper); }