Javascript timer - You generated the link "X seconds ago"

211 Views Asked by At

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!)

2

There are 2 best solutions below

0
On BEST ANSWER

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);
0
On

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>