0

based on my previous question (HERE) ive updated my time ago code block, it works fine,the interval gets called every 10 sec but im faced with yet another silly problem . My problem is that i dont know how to load the function as soon as the body is loaded

my code (the important part)

  $(".elapsed_time").each(function() {
     time_r = $(this).data('time_raw');
    var self = $(this);
    var inter = function() {self.html(time_ago(time_r));}
    setInterval(inter, 10000);
  }); 
Community
  • 1
  • 1
Dev Man
  • 2,114
  • 3
  • 23
  • 38
  • @Unknown it not a possible duplicate besides ive tried the solutions on that question none work – Dev Man May 07 '14 at 17:35

1 Answers1

0

This can be done in jQuery by wrapping your code in the following:

$(document).ready(function () {
  //-- This code will run when the body is loaded
});

You can also use the shorthand notation to accomplish the same thing:

$(function () {
  //-- This code will run when the body is loaded
});

Edit: Kevin raises a good point, this is technically different from a true 'body load' event. The way I interpreted your issue, you wanted to wait until all of the elements are available before running your code.

More info on this: window.onload vs $(document).ready()

Community
  • 1
  • 1
Robert Messerle
  • 3,022
  • 14
  • 18