PDA

View Full Version : counting time from a user action


Vaupell
02-20-2009, 02:09 PM
i want to count 10 minuttes from a action.

A user clicks a botton / or another event happens that triggers a timestamp
to be marked.

how would i go about counting 10 minuttes, and activate a trigger after
those 10 minuttes ?

sort of a delay, and perhaps ewen if the user hits refresh
the display would just show something like 5minuttes togo.

all i need is the counting, i have no clue or no idea where to begin. ;)

Dismounted
02-21-2009, 03:31 AM
And this is used for?

vbplusme
02-21-2009, 04:13 AM
I think you might need to use a javascript function to get were you want to go.

TigerC10
02-21-2009, 05:34 AM
I think you might need to use a javascript function to get were you want to go.

That would require a user to stay on the page for 10 minutes and not navigate away. Very not cool from a design standpoint.


A user clicks a botton / or another event happens that triggers a timestamp to be marked.


Very easy to add a timestamp to a database.


how would i go about counting 10 minuttes, and activate a trigger after
those 10 minuttes ?


Not very easy to make a "trigger" after 10 minutes. You might consider using what's known as a CRON job. CRON jobs are scripts that get executed every 5/10/15/30/60/1440 minutes. There's no real way to guarantee that the trigger will be executed exactly 10 minutes after someone sets the timestamp - the cron job will just run at the set intervals. So if someone initaties the trigger right before your cron job runs - then the script will pass them over since it hasn't been 10 minutes for them. They may have to wait 12 minutes, or even 15... All the way up to 19 minutes for the next cron job script to come along and unset the delay.


sort of a delay, and perhaps ewen if the user hits refresh
the display would just show something like 5minuttes togo.


Okay, hang on. Do you want a trigger or do you just want a delay? Because delays are much easier...

Say you've got a table in the database called "delay_timestamps"... It would have the fields "userid" and "timestamp".

Insert or update the field with the user like so

$db->query_write("INSERT INTO ".TABLE_PREFIX."delay_timestamps (userid, timestamp) VALUES ".$userid.", ".time() );
// or...
$db->query_write("UPDATE ".TABLE_PREFIX."delay_timestamps SET timestamp=".time()." WHERE userid=".$userid );


// OR even better - this handles insert and update in 1 SQL statement
// but you have to set the userid field in the table as the key
// shouldn't be a problem since userids should be unique anyway

$db->query_write("INSERT ON DUPLICATE KEY UPDATE INTO ".TABLE_PREFIX."delay_timestamps (userid, timestamp) VALUES ".$userid.", ".time() );


Then in your "refresh" page you'll want to have a look see at the timestamp in the database and compare it with the current time:


$wait_time = 5; // set the wait time they have before accessing the page again

$current_time = time();

$user_timestamp = $db->query_first("SELECT timestamp FROM ".TABLE_PREFIX."delay_timestamps WHERE userid=".$userid );

$number_of_seconds_since_stamp = $current_time - $user_timestamp;

$minutes_since_timestamp = $number_of_seconds_since_stamp / 60;

if($minutes_since_timestamp < $wait_time)
{
echo 'Sorry, you still have '. $wait_time-$minutes_since_timestamp .' minutes left...';
}
else
{
echo 'How sweet, you waited for me!';
}



This, of course, isn't the exact code you'll want... Certainly you'll want to condense the code down, like the $number_of_seconds_since_stamp variable is totally worthless - you can just divide by 60 on the fly. The timestamp variable in the database should probably be of type INTEGER, be careful not to use something like SMALLINT because it's not big enough for the timestamp. However, this will require an action from your user - it's not a trigger. It will require the user to click refresh, or to view the page again in some way.

You might be able to work out some AJAX thing that starts up on every single page that checks for the time remaining and then starts an auto-refresh count down... But it'd be kinda wasteful in my opinion. Plus, some browsers disable the javascript redirects/refreshes.

vbplusme
02-21-2009, 07:14 AM
At this point, I am with DM and wondering now what the problem is that needs to be solved?

TigerC10
02-21-2009, 10:39 AM
At this point, I am with DM and wondering now what the problem is that needs to be solved?

Eh, I guess I'm a programmer at heart. I don't care what I program, I just need a directive. Haha.

But, judging from his recent posts, it looks like he's trying to set a timer on submitting scores to ibProArcade - maybe to prevent cheating...

vbplusme
02-21-2009, 12:24 PM
Got it. Now, it makes a lot of sense to me, thanks for that update.

Vaupell
02-23-2009, 04:45 PM
hi guys, hehe well i have made a small textbased rpg game like the ones you know from
around the web and facebook, but running on vbulletin.
but needed somekind of limit of actions :D turns pr day or turns pr hour,
and thought well turns pr 10 minuttes would be the way to go.

but got stuck on another project so havent been looking into it the lasy couple of days.