Kaynağa Gözat

hw14 eshe ne done

Vadym Hlushko 3 yıl önce
ebeveyn
işleme
6b968da284
1 değiştirilmiş dosya ile 78 ekleme ve 14 silme
  1. 78 14
      hw14/main.js

+ 78 - 14
hw14/main.js

@@ -19,11 +19,7 @@ function jsonPost(url, data)
 }
 
 let sendBtn = document.getElementById('sendButton')
-sendBtn.onclick = function sendMsg () {
-    let nick = document.getElementById('nickInput').value
-    let msg = document.getElementById('msgInput').value
-    jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nick, message: msg })
-}
+
 sendBtn.innerHTML = "Send"
 let nickInput = document.getElementById('nickInput')
 let msgInput = document.getElementById('msgInput')
@@ -39,11 +35,60 @@ nickInput.style.fontSize = '20px'
 msgInput.style.fontSize = '20px'
 sendBtn.style.fontSize = '20px'
 
-function addMsg () {
-    let startMsg = 0;
-    setInterval(function(){
-        jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: startMsg}).then (result => {console.log(result.data)
-        for(startMsg in result.data) {
+// default
+// sendBtn.onclick = function sendMsg () {
+//     let nick = document.getElementById('nickInput').value
+//     let msg = document.getElementById('msgInput').value
+//     jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nick, message: msg })
+//     document.getElementById('nickInput').value = ''
+//     document.getElementById('msgInput').value = ''
+// }
+
+// function addMsg () {
+//     let startMsg = 0;
+//     setInterval(function(){
+//         jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: startMsg}).then (result => {console.log(result.data)
+//         for(startMsg in result.data) {
+//             let chat = document.getElementById('chat')
+//             let divMessage = document.createElement('div')
+//             divMessage.style.border = '1px solid black'
+//             divMessage.style.borderRadius = '10px'
+//             divMessage.style.marginTop = '10px'
+//             divMessage.style.backgroundColor = '#1E90FF'
+//             divMessage.style.color = 'white'
+//             let pMsg = document.createElement('p')
+//             pMsg.innerHTML = result.data[startMsg].nick + ':' + ' ' + result.data[startMsg].message
+//             pMsg.style.marginLeft = '20px'
+//             pMsg.style.fontSize = '30px'
+//             let time = document.createElement('p')
+//             time.innerHTML = `${new Date(result.data[startMsg].timestamp).toLocaleString()}`
+//             time.style.marginLeft = '20px'
+//             divMessage.append(pMsg)
+//             divMessage.append(time)
+//             chat.prepend(divMessage)
+//         }
+//         startMsg = result.nextMessageId})
+//     },2000)
+// }
+// addMsg()
+
+//async
+function delay(ms) {
+    return new Promise(resolve => setTimeout(resolve, ms));
+  }
+
+  
+  async function sendMessage(nick,message) {
+    jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nickInput.value, message: msgInput.value })
+    document.getElementById('nickInput').value = ''
+    document.getElementById('msgInput').value = ''
+    }
+
+    async function getMessages() {
+        let startMsg = 0
+        jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: startMsg})
+        .then(result=>{console.log(result.data)
+        for(startMsg in result.data){
             let chat = document.getElementById('chat')
             let divMessage = document.createElement('div')
             divMessage.style.border = '1px solid black'
@@ -62,8 +107,27 @@ function addMsg () {
             divMessage.append(time)
             chat.prepend(divMessage)
         }
-        startMsg = result.nextMessageId})
-    },2000)
-}
-addMsg()
+    })
+    } 
+
+    async function sendAndCheck(){
+        await sendMessage(nick,message)
+        await getMessages()
+    }
+
+    sendBtn.onclick = () => {
+        sendAndCheck()
+    }
+
+    async function checkLoop (){
+        delay(3000)
+        getMessages()
+    }
+
+    checkLoop()
+
+
+
+
+