Hi,
What I’m trying to do is that each time the function is called (when a message is received from the server), a variable is incremented by 1 from 49. Why? To create a new div for the new message.
Right now, i only return 50 each time, which is not what I want
How the function is called:
ws.onmessage = function(event){
let newMessage = event.data;
let messageRecopie = JSON.parse(newMessage);
messageRecu(messageRecopie);
};
And how I tried to increase i each time:
function messageRecu(obj){
let chatMessage = obj;
let i = (function(n){
n += 1;
return n;
}(49));
console.log(i);
let chatWindow = document.getElementById('chat');
let ligneChat = document.createElement('DIV');
ligneChat.setAttribute('id','ligneChat'+i);
chatWindow.appendChild(ligneChat);
(...)
I want to increment i each time a new message is received starting from 50 to infinite.
I don’t know if this is what you want, but ws is declared like this:
function connectionWebSocket(token){
let ws = new WebSocket("ws://message.com:8000/ws");
...
ws.onmessage = function(event){
let newMessage = event.data;
let messageRecopie = JSON.parse(newMessage);
messageRecu(messageRecopie);
};
...
This would go inside the connectionWebSocket function. Without seeing the entire code for your project, I can only suggested moving messageRecu inside connectionWebSocket. Where else outside of connectionWebSocket would you even call messageRecu, since ws lives inside connectionWebSocket?