Skip to content

Instantly share code, notes, and snippets.

@cameck
Created May 6, 2016 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameck/b25d6330dcfa7134ae79337a28404803 to your computer and use it in GitHub Desktop.
Save cameck/b25d6330dcfa7134ae79337a28404803 to your computer and use it in GitHub Desktop.
Scripts for the Loneliest Chat room
$(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