5

I have a javascript function

function relativeTime(time) {

var period = new Date(time);
var delta = new Date() - period;

if (delta <= 10000) {   // Less than 10 seconds ago
    return 'Just now';
}

var units = null;

var conversions = {
    millisecond: 1,     // ms -> ms
    second: 1000,   // ms -> sec
    minute: 60,         // sec -> min
    hour: 60,       // min -> hour
    day: 24,        // hour -> day
    month: 30,      // day -> month (roughly)
    year: 12            // month -> year
};

for (var key in conversions) {
    if (delta < conversions[key]) {
        break;
    }
    else {
        units = key;
        delta = delta / conversions[key];
    }
}

// Pluralize if necessary:

delta = Math.floor(delta);
if (delta !== 1) { units += 's'; }
return [delta, units, "ago"].join(' ');

}

that give relative time difference like facebook comment.

How can i call this function in my view. I am using mvc3. i am getting time from database like,

        <span>
            @item.wallTimeStamp
        </span>

instead of that i want to call javascript function,

i have a tag

<span>
    //call javascript function that will display time difference in this tag
   </span>

how can i do that?

1
  • 8
    Sir, you have 8 Questions in your profile and you have not accepted a single one of them. You do not seem to reward others that help you :( Commented Sep 5, 2011 at 10:41

4 Answers 4

6
$(document).ready(function() {
 //called when the document is ready
});
Sign up to request clarification or add additional context in comments.

4 Comments

which one? You wanted to execute a js function when the document is loaded - you can execute it in the ready(function() {..})
This is JQuery syntax, Bhargav clearly isn't using the JQuery framework.
@Berry Ligtermoet he tagged it. Twice ;)
Yea my apologies, didnt see the tags :S
5

First, give the span id(or something else for identification)

<span id="relativeTime">
</span>

Then, using javascript, first calculate that relative time value using your function. Then, using jQuery.text(), set that value inside span

<script>

$(document).ready(function() {
  var wallTimeStamp = '@item.wallTimeStamp';

  var relativeTimeValue = relativeTime(wallTimeStamp);

  $('#relativeTime').text(relativeTimeValue);
});

</scipt>

Code modification may be needed for multiple spans of that kind

5 Comments

What is the error? If there's error or parsing @item.wallTimeStamp string as Date on client side, you may need to set correct format for it. That's upon you :)
Not getting value from database in var wallTimeStamp = '@item.wallTimeStamp'; how can i get @item.wallTimestamp value in function? Is there any way to get this?
Do you use Razor view engine? And during debug, do you really see that propety value is not null?
Yes i am using Razor view engine. i am not able to debug javascript function.
I meant debugging on server side. I tested exactly that code and it's working fine (setting value in source)
4

To be sure that your javascript code runs when the DOM has loaded you must use the ready() method. Look here for some explanation. You can use also this shortahand notation for it:

$(function() {
    //put your code here
});

Comments

2
<body onload="javascript:relativeTime(time);">

where time is whatever you want it to be.

If you want it as a clickable thing with an assigned value from your model, then do something like

<span onclick="relativeTime(@item.time)">@item.wallTimeStamp</span>

edit:

alright..

assuming your time in the function is coming from your model:

@HiddenFor(m => m.time)

create a seperate js file, and link it in your html doc:

<script type="text/javascript" src="whatever.js"></script>

inside it (assuming you mean jquery):

$(function() {
    relativeTime($("#time").val());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.