I need to sleep the code until some condition is met or a 3 second timeout is passed. then return a simple string. Is there anyway I can do this?
// this function needs to return a simple string 
function something() { 
    var conditionOk = false;
    var jobWillBeDoneInNMiliseconds = Math.floor(Math.random() * 10000);
    setTimeout(function() {
        // I need to do something here, but I don't know how long it takes
        conditionOk = true; 
    }, jobWillBeDoneInNMiliseconds);
    // I need to stop right here until
    // stop here until ( 3000 timeout is passed ) or ( conditionOk == true )
    StopHereUntil( conditionOk, 3000 );
    return "returned something"; 
}
here is what I exactly going to do:
I make the browser scroll to bottom of the page, then some ajax function will be called to fetch the comments (that I have not control on it). Now I need to wait until comments are appeared in document with ".comment" class.
I need the getComments() function return comments as a json string.
function getComments() {
    window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);
  var a = (document.querySelectorAll('div.comment'))
  // wait here until  (  a.length > 0  ) or ( 3 second is passed )
  // then I need to collect comments
  var comments = [];
  document.querySelectorAll('div.comment p')
    .forEach(function(el){      
        comments.push(el.text());
    });
  return JSON.stringify(comments);
} 
getComments();
 
     
     
     
     
     
     
    