// This function runs when the page is loaded
$(document).ready(function() {
var timestamp = Date.now();
var sender = Number(timestamp);
if(sessionStorage.getItem("username") == null){
var text = 'start_session';
} else {
var text = 'has_name';
}
setTimeout(function() {
randbotMPS(text,sender,timestamp);
}, 200);
sessionStorage.started_on = timestamp;
sessionStorage.message_count = 0;
// Automatically accept the enter key
$('input:first').keypress(function(e) {
if (e.keyCode == 13) {
$('input:button').click();
$('input:first').val(''); // Clear for next payload
}
});
});
// This captures the onclick event on submit button
$('input:button').click(function() {
var text = $("input:first").val(); // input text
var sender = Number(sessionStorage.started_on);
var timestamp = Date.now();
if(text != '') {
text = $.trim(text); // trim white space
show(text,'You',timestamp);
randbotMPS(text,sender,timestamp);
}
sessionStorage.message_count++;
});
// This shows the message in the chat box
function show(text,sender,timestamp) {
var ts = new Date(timestamp).toLocaleTimeString();
$('#chatbox').append("<div class='message new'></div>");
$('.message.new').append("<p class='sender'>" + sender + "</p>");
$('.message.new p').append("<span class='time'>" + ts + "</span>");
$('.message.new').append("<div class='text'>" + text + "</div>");
if(sender == 'Bot') {
$('.message.new').addClass("sent");
} else {
$('.message.new').addClass("received");
}
$('.message.new').removeClass("new");
$('#chatbox').scrollTop($('#chatbox')[0].scrollHeight);
}
// function to call MATLAB Production Server to generate response
function randbotMPS(text,sender,timestamp) {
var msg = {
message: {
text: text,
sender: sender,
timestamp: timestamp
}
}
// Create a simple AJAX request to the MATLAB Production...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.