Home Start countdown after php page load
Reply: 1

Start countdown after php page load

Emoless96
1#
Emoless96 Published in 2017-12-07 17:07:12Z

I am trying to create a browser training button. When the user clicks the " Brench Press" button his "Strength" increases by a php file that i've set in the form action and it checks some values and inserts values to the database that shows up.

What my problem and what i would like some help with is to implement my clock function that sets a countdown from 30 seconds to 0. This should be a countdown that the users has to watch and wait for untill he can "train" again. So somehow i need to write this clock function to output after the php file has been loaded and not onclick on the button as of now. And also for the clock not to refresh it's countdown on page refresh but keep going.

What i tried to do is to create a session with the function call inside and send it to the site so it loads after the php file has loaded but it doesn't seem to work.

$_SESSION['CountDownBenchPress'] = "<script> countdown(); </script>";

var time = 30;
function countdown()
{
  if(time==0)
  {
    window.location.reload();
  }
  else
  {
    tmptime = time;
    dayTime = 24*60*60;
    hourTime = 60*60;
    minutesTime = 60;

    days = Math.floor(tmptime/dayTime);
    tmptime = tmptime - days*dayTime;

    hours = Math.floor(tmptime/hourTime);
    tmptime = tmptime - hours*hourTime;

    minutes = Math.floor(tmptime/minutesTime);
    tmptime = tmptime - minutes*minutesTime;

    seconds = tmptime;
    tidtext="";
    if(days>0)
      tidtext = days+ hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else if(hours>0)
      tidtext = hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else
      tidtext =  minutes + " minutes " + seconds + " seconds";

    document.getElementById('timer').innerHTML = tidtext;
    time--;
    setTimeout("countdown()", 1000);
  }
}
<form class="" action="" method="">
            <td data-th="Workout"><input type="submit" onclick="countdown()" name="" value="Bench Press"></td>

            <div id="timer"></div>

          </form>

Orange Orange
2#
Orange Orange Reply to 2017-12-07 17:36:48Z

var time = 30;
function countdown()
{
  if(time==0)
  {
    window.location.reload();
  }
  else
  {
    tmptime = time;
    dayTime = 24*60*60;
    hourTime = 60*60;
    minutesTime = 60;

    days = Math.floor(tmptime/dayTime);
    tmptime = tmptime - days*dayTime;

    hours = Math.floor(tmptime/hourTime);
    tmptime = tmptime - hours*hourTime;

    minutes = Math.floor(tmptime/minutesTime);
    tmptime = tmptime - minutes*minutesTime;

    seconds = tmptime;
    tidtext="";
    if(days>0)
      tidtext = days+ hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else if(hours>0)
      tidtext = hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else
      tidtext =  minutes + " minutes " + seconds + " seconds";

    document.getElementById('timer').innerHTML = tidtext;
    time--;
    setTimeout("countdown()", 1000);
  }
}
<form class="" action="" method="">
            <td data-th="Workout"><input type="button" onclick="countdown()" name="" value="Bench Press"></td>

            <div id="timer"></div>

          </form>

you must put input type="button" instead of submit. That's because the submit redirects you to the action page that you wrote in the form (in your case, nothing)

EDIT I didn't notice the auto-click part. To solve it, you must modify the body tag as it follows

<body onload="document.getElementById('clickme').click()">

and the button like this

<input type="button" id="clickme" onclick="countdown()" name="" value="Bench Press">
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304373 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO