javascript - PHP Countdown Timer action

112

Hi all apologies if this is an easy solution, I am creating an test application and require that a timer is started when the user clicks on the test. I am however completely confused how to implement a counter for the application - and how to trigger an action when the timer has reached 0.

I have found implementations of JS timers/countdowns:

PHP Timer wait 30 seconds then run a command

PHP when countdown runs out

and a php timer:

Start and stop a timer PHP

I was hoping that someone could help me with an example of a PHP timer that triggers an action when it has reached 0 for example - redirecting to another page.

Many thanks in advance - please have patience, I am a total beginner

351

Answer

Solution:

Using JQuery:

function runTimer(seconds, yourUrl) {
  setTimeout(function() {
    $.get(yourUrl);
  }, seconds * 1000;
}

run it:

runTimer(5, 'http://example.com/action.php');

But it won't be possible to redirect to other page using ajax request.

For redirecting use:

function runTimerRedirect(seconds, yourUrl) {
  setTimeout(function() {
    window.location = yourUrl
  }, seconds * 1000;
}

People are also looking for solutions to the problem: php - mongodb extension is installed but icannot reflect changes in mongoDB console

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.