-1

How can I make a text display "You generated the link "X seconds ago" after you generate a link. It's part of a website for file-hosting.

I've tried googling, but haven't come to anything that answers my questions.

I'm not even sure if it is Javascript (I'm just assuming!)

JoeIsBlogging
  • 185
  • 1
  • 4
  • 12
  • `setInterval(function(){timeAgo = timeAgo +1;},1000);` this will create a timer that will increment a `timeAgo` var every 1000 milliseconds. – Banana Jun 09 '15 at 18:34
  • If i understand this correctly, users in your website create some link and you want to show them the time when they created it. So at the time of creation, why dont you store this value in the database and then query for this value using AJAX or just refresh the page. – Rash Jun 09 '15 at 18:34
  • You may want to review [this question and its answers](http://stackoverflow.com/q/729921/215552) for why the answers may not be as accurate as you might like. – Heretic Monkey Jun 09 '15 at 18:46
  • possible duplicate of [JavaScript: How to do something every full hour?](http://stackoverflow.com/questions/12309019/javascript-how-to-do-something-every-full-hour) – Heretic Monkey Jun 09 '15 at 18:50

2 Answers2

2

use setInterval in order to show the user how many seconds since the link generation:

HTML:

<!-- We assume here that the link is already created. -->
<p>You generated the link <span id="timer"></span> seconds ago</p>

JS:

var timerSpan = document.getElementById('timer'), seconds = 0;
setInterval(function() {
    seconds++;
    timerSpan.innerHtml(seconds);
}, 1000);
taxicala
  • 21,408
  • 7
  • 37
  • 66
2

This should help:

document.getElementById('createLink').addEventListener('click', function generateLink (event) {
  var timeAgo = Date.now();

  this.removeEventListener('click', generateLink);

  function render () {
    var timeDiff = Math.floor((Date.now() - timeAgo) / 1000);

    document.getElementById('notice').textContent = 'You generated a link ' + timeDiff + ' seconds ago.';
  }

  setInterval(render, 1000);
  render();
});
<button id="createLink">Generate Link</button>
<span id="notice"></span>
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153