Created
May 6, 2016 23:37
-
-
Save cameck/b25d6330dcfa7134ae79337a28404803 to your computer and use it in GitHub Desktop.
Scripts for the Loneliest Chat room
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var newChat = function() { | |
var author = ["Me", "Myself", "I"]; | |
var randomNumber = Math.floor(Math.random() * 3); | |
var time = new Date(); | |
time = time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); | |
console.log(time); | |
var newMessage = $('#new-message-body').val(); | |
var messageFormat = '<li class="message">' + | |
"<a class='delete' href='#'>Delete</a>" + | |
"<h3 class='author'>" + author[randomNumber] + "</h3>" + | |
'<p class="message-body">' + newMessage + '</p>' + | |
'<span class="timestamp">Posted at: ' + time + '</span>' + | |
'</li>'; | |
$('#conversation').append(messageFormat); | |
$('#new-message-body').val(''); | |
}; | |
//Click | |
$('#new-message-button').on('click', function() { | |
newChat(); | |
}); | |
//delete message | |
$('#conversation').on('click', '.delete', function(){ | |
$(this).closest('.message').fadeOut(); | |
}); | |
//press enter to send message | |
$('#new-message-body').keyup(function(e){ | |
if(e.keyCode === 13) { | |
newChat(); | |
} | |
}); | |
//Lonely Joke maker | |
$('#lonely').on('click', function(){ | |
//Get chuck norris joke | |
$.get('http://api.icndb.com/jokes/random', function() { | |
joke = arguments[0].value.joke; | |
}); | |
var time = new Date(); | |
time = time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); | |
var messageFormat = '<li class="message">' + | |
"<a class='delete' href='#'>Delete</a>" + | |
"<h3 class='author'>CHUCK</h3>" + | |
'<p class="message-body">' + joke + '</p>' + | |
'<span class="timestamp">Posted at: ' + time + '</span>' + | |
'</li>'; | |
$('#conversation').append(messageFormat); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment