-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (45 loc) · 1.31 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function prepareForm()
{
beginListening();
$('#sendform').submit(function(){
//disable the Send button while the message is being sent
$('#sendSubmit').val('Sending...');
$('#sendSubmit').attr('disabled',true);
//issue the AJAX request
$.ajax({
url: "send.php",
type: "POST",
data: {name:$('#sendname').val(),message:$('#sendmessage').val()},
success: function(resp){
if (resp != "sent") //Some failure occured
{
$('#sendstatus').text('Failed').slideDown('slow').delay(1500).slideUp('slow');
}
else //successful
{
//add the just sent message to the chat box
var disp = "You:\n" + $('#sendmessage').val() + "\n";
$('#listenmessage').val($('#listenmessage').val() + "\n" + disp);
$('#sendmessage').val('');
}
//enable the send button
$('#sendSubmit').val('Send');
$('#sendSubmit').removeAttr('disabled');
}
});
return false;
});
}
function beginListening()
{
var ajaxlisten = $.ajax({
url: "receive.php",
type: "POST",
success: function(resp){
var msg = JSON.parse(resp);
var display = msg['sender'] + ":\n" + msg['message'] + "\n";
$('#listenmessage').val($('#listenmessage').val() + "\n" + display);
beginListening();
}
});
}