0

I am trying to create an array that pass one item waits for a response from my node server from a list.

My data input to JavaScript is from a text area in html then I am trying to send each line one at a time it can only send the next array item when my nodeJS has finished can anyone show me any example's or ways to post an array one item at a time.

instead of in one big chunk like I am getting currently.

<script src="js/socket.io.js"></script>
<script type="text/javascript">
function textareaToArray(t){
    return t.value.split(/[\n\r]+/);
}
function showArray(msg){
    for(i = 0; i < msg.length; i++) {
        // something per item
        socket.emit("dout", { message : msg[i] } );


    }
    // the old code
    // document.getElementById("message").innerHTML = msg.join("&#013;");
}
</script>
<script>
var socket = io.connect("https://json2-c9-ashg1990.c9.io");
socket.on("news", function(data) {
document.getElementById("message").innerHTML = JSON.stringify(data.hello);

});
// socket.emit("my other event", { message : "client emit" } );
</script>

my full html

<html>
<html>

<head>

<title>Welcome To ....</title>
<script src="js/socket.io.js"></script>
<script type="text/javascript">
function textareaToArray(t){
    return t.value.split(/[\n\r]+/);
}
function showArray(msg){
    for(i = 0; i < msg.length; i++) {
        // something per item
        socket.emit("dout", { message : msg[i] } );


    }
    // the old code
    // document.getElementById("message").innerHTML = msg.join("&#013;");
}
</script>
<script>
var socket = io.connect("https://json2-c9-ashg1990.c9.io");
socket.on("news", function(data) {
document.getElementById("message").innerHTML = JSON.stringify(data.hello);

});
// socket.emit("my other event", { message : "client emit" } );
</script>

</head>

<body>

<h1> WELCOME TO .... </h1>

<form>

<textarea rows="10" cols="60" name="alpha"></textarea>

<br>

<input type="button" value="show array" onclick="showArray(textareaToArray(this.form.alpha ))">

</form>

<br>

<textarea id="message" rows="6" cols="60" name="message"></textarea>

</body>

</html>
4
  • Your code is a mess. Can you please post the old code separately from what you have now? Commented Feb 26, 2014 at 18:16
  • the old code is this code and I am new to javascript hence the mess if you could point me in the right direction or what I am looking for i.e a name for the type of array function I want I tthink I may have to use callback() Commented Feb 26, 2014 at 18:18
  • I just cannot get what your code is doing and what you are trying to accomplish. Where do you use textareaToArray? Where do you actually send the array? I can see only showArray function which is used nowhere. Commented Feb 26, 2014 at 18:22
  • I am sending the array to node js using the socket.emit function . I have edited the post with my html and node server Commented Feb 26, 2014 at 19:19

1 Answer 1

0

Well, you code seems OK, except the regexp you use. You should split the string using \n character. You are using \n\r, which is not a Windown line break character sequence.

Window character sequence is \r\n.

See here: http://www.regular-expressions.info/nonprint.html

1
  • The regexp is not \n\r, it's [\n\r]. So, it's a correct one. Commented Feb 27, 2014 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.