Joe Pigott
Joe Pigott

Reputation: 8459

Providing Lapsed Time on "Social Network" Post

jsFiddle.

I have created a little javascript snippet that can take two inputs and make a sort of "post" using those. In those posts, it says something like:

Hi!

Posted by Random Person _ minutes ago.

but without the underscore, it should say the lapsed time between now and the time posted. I am having difficulty thinking of how to do this, but this is what I am currently using:

$('#b').click(function () {
    var v = $('#type').val();
    var u = $('#input').val();
    if (v !== "" && u !== "") {
        var time = new Date();
        var currentime = Date.now();
        var x = currentime - time;
        $("ul").prepend("<li>" + v + "<br />Posted by " + u + " " + x + " minutes ago  </li>");
        $('#type, #input').css('border', '');
    } else if (v == "" && u == "") {
        $('#type, #input').css('border', '1px solid red');
    } else if (v == "") {
        $('#type').css('border', '1px solid red');
        $('#input').css('border', '');
    } else {
        $('#input').css('border', '1px solid red');
        $('#type').css('border', '');
    }
});
#type, #input {
    border-radius: 10px;
    background: #dadae3;
    color: #59ACFF;
    border: 1px solid #dadae3;
}
#type {
    border-bottom-right-radius: 0;
}
#type:hover, #input:hover {
    background: #c4c4cc;
    color: #488CCF;
    border: 1px solid #c4c4cc;
}
#type:hover::-webkit-input-placeholder {
    color: #59ACFF
}
#input:hover::-webkit-input-placeholder {
    color: #59ACFF
}
#type:focus, #input:focus {
    border: 1px solid #59ACFF;
    outline: 0;
}
button {
    height: 30px;
    background: #dadae3;
    border-radius: 10px;
    border: 1px solid #dadae3;
    color: #59ACFF;
    cursor: pointer;
}
button:hover {
    background: #c4c4cc;
    color: #488CCF;
    border: 1px solid #c4c4cc;
}
button:focus {
    outline: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<br />
<textarea id='type'></textarea>
<br />
<br />
<input id='input'>
<br />
<br />
<button id='b'><span class='fa fa-plus-square-o fa-2x'></span>

</button>
<ul id='ul'></ul>

I realize that this is wrong, but I cannot think of other ways to do this. I would like the lapsed time to be updated every minute, also.

Thank you. :)

Upvotes: 2

Views: 103

Answers (2)

Ben
Ben

Reputation: 1568

Here's my implementation of it. The updatePost function is called every 10 seconds to update the time in the post. Slight adjustments are also made to the code (eliminating repeated $('#type, #input').css('border', '');, etcetera).

Remember to open your web console to view debug outputs.

This can also be found on JSFiddle.

Update:

  1. Separated setInterval function upon Martin's suggestion.
  2. The new example now takes advantage of the data attribute.

// Update the posts every 10 seconds
setInterval(function () {
    console.log('Updating posts...');
    $('ul .time').each(function (id, span) {
        time = Math.round((new Date() - $(span).data('timestamp')) / 60000);
        $(span).text(time);
    });
}, 10000);

$('#b').click(function () {
    var v = $('#type').val();
    var u = $('#input').val();
    $('#type, #input').css('border', '');

    if (v !== "" && u !== "") {
        // Generate a timestamp for every post
        var timestamp = Date.now();

        $("ul").prepend('<li>' +
            '<span class="body">' + v + '</span><br />' +
            'Posted by ' +
            '<span class="author">' + u + '</span> ' +
            '<span class="time" data-timestamp="' + timestamp + '">0</span>' +
            ' minutes ago  </li>');

    } else {
        if (v == "") {
            $('#type').css('border', '1px solid red');
        }
        if (u == "") {
            $('#input').css('border', '1px solid red');
        }
    }
});
#type,
#input {
  border-radius: 10px;
  background: #dadae3;
  color: #59ACFF;
  border: 1px solid #dadae3;
}
#type {
  border-bottom-right-radius: 0;
}
#type:hover,
#input:hover {
  background: #c4c4cc;
  color: #488CCF;
  border: 1px solid #c4c4cc;
}
#type:hover::-webkit-input-placeholder {
  color: #59ACFF
}
#input:hover::-webkit-input-placeholder {
  color: #59ACFF
}
#type:focus,
#input:focus {
  border: 1px solid #59ACFF;
  outline: 0;
}
button {
  height: 30px;
  background: #dadae3;
  border-radius: 10px;
  border: 1px solid #dadae3;
  color: #59ACFF;
  cursor: pointer;
}
button:hover {
  background: #c4c4cc;
  color: #488CCF;
  border: 1px solid #c4c4cc;
}
button:focus {
  outline: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<br />
<textarea id='type'></textarea>
<br />
<br />
<input id='input'>
<br />
<br />
<button id='b'><span class='fa fa-plus-square-o fa-2x'></span>

</button>
<ul id='ul'></ul>

PS: enhancements such as differentiating 'Minutes' and 'Minute' should also be implemented.

Upvotes: 3

Martin E. Zulliger
Martin E. Zulliger

Reputation: 849

What about this tiny modification to your code?

The trick is to store a custom param in a span with the time and then update regularly.

$('#b').click(function () {
    var v = $('#type').val();
    var u = $('#input').val();
    if (v !== "" && u !== "") {
        $("ul").prepend("<li>" + v + "<br />Posted by " + u + " <span posted=\""+ currentime + "\">0</span> minutes ago  </li>");
        $('#type, #input').css('border', '');
    } else if (v == "" && u == "") {
        $('#type, #input').css('border', '1px solid red');
    } else if (v == "") {
        $('#type').css('border', '1px solid red');
        $('#input').css('border', '');
    } else {
        $('#input').css('border', '1px solid red');
        $('#type').css('border', '');
    }
});

setInterval(function() {
    $('ul span').each(function(id, span) {
        time = Math.round((new Date() - $(span).attr('posted')) / 60000);
        $(span).text(time);
    });
}, 5000);
#type, #input {
    border-radius: 10px;
    background: #dadae3;
    color: #59ACFF;
    border: 1px solid #dadae3;
}
#type {
    border-bottom-right-radius: 0;
}
#type:hover, #input:hover {
    background: #c4c4cc;
    color: #488CCF;
    border: 1px solid #c4c4cc;
}
#type:hover::-webkit-input-placeholder {
    color: #59ACFF
}
#input:hover::-webkit-input-placeholder {
    color: #59ACFF
}
#type:focus, #input:focus {
    border: 1px solid #59ACFF;
    outline: 0;
}
button {
    height: 30px;
    background: #dadae3;
    border-radius: 10px;
    border: 1px solid #dadae3;
    color: #59ACFF;
    cursor: pointer;
}
button:hover {
    background: #c4c4cc;
    color: #488CCF;
    border: 1px solid #c4c4cc;
}
button:focus {
    outline: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<br />
<textarea id='type'></textarea>
<br />
<br />
<input id='input'>
<br />
<br />
<button id='b'><span class='fa fa-plus-square-o fa-2x'></span>

</button>
<ul id='ul'></ul>

Upvotes: 0

Related Questions