You can always use the Timer
command and compare the times to get the "difference". The problem you have is that ASP classic is server side single directional, meaning it has no two way connection with the client. Data is only exchanged with the server when the page is loaded or refreshed. You could include some javascript options that would allow for some transactions to happen without the page needing to be reloaded. The method using the database to keep track of the time is going to be useful, because every time you reload the page, the Timer
would be reset.
<% 'Variables
dim StartTime, StopStop, HowLong
'set the StartTime
StartTime = Timer
'your function goes here
'set the StopTime
StopTime = Timer 'stops the timer
'set HowLong to be equal to StopTime subtract Start Time
HowLong = StopTime - StartTime
'Display the results to screen
Response.Write ("It took " & HowLong & " seconds to complete.")
%>
Now that script was just to show you how you can track how long time has passed. I think I explained the limitations of your problem with ASP Classic being only Server Side, so you would need to use a method like you were thinking about where you were constantly outputting that HowLong variable and comparing it to the database.
One last thing to consider, navigating away from the page or refreshing the page will retrigger the script, so you will want to put in some failsafe measures in place to avoid unwanted post backs or other complications since we don't know what your script does. If you give me some more information, I might be able to offer more advice, but without seeing what your code is doing, I hope this is enough to get you pointed in the right direction.
EDIT - One more thought is you can use the Session
object to store data in between page loads. You could use this to compare the timer against the one in the database. Session
objects are also secure and can not be modified manually by a user, so it would be a secure way to pass data between page loads. Here is some sample code on how to store into a Session
variable and pull from it. Let's pretend we are using both scripts at the same time so that I can pass and store HowLong into a Session
variable. Although you will require the database as it is not a long term storage, it will time out in a few minutes, and you can specify how long they should last if you want them to last a long time even while idle. Now to the code.
<%
'store into a session variable
HowLong = Session("HowLong")
'read from a session variable
Session("HowLong") = HowLong
%>
Hope that edit added the extra bit you might of needed. I think I basically just explained the process of how to do what they were saying. If you include the Session
method you could use that to store the data so you only have to write or read from the database every few minutes or so.
OHH EDIT - I forgot to mention Timer
calculates in seconds, or how many seconds have passed since a date in the 80's. So although you can compare dates and timestamps to get time, if you are trying to see how long something started and stopped, Timer
is the simplest method without having to worry about datatypes or anything like that. And one last thing about Session variables, if you close the browser, they go poof. I don't think that applies to tabs but curious to know. But if you use this timer, database, session method, it can track when it starts but getting it to detect the precise disconnect time without constant database checks would be the issue. It's just like, you are better off maybe using something in javascript or Asp.net to do it the non-convoluted way, but I know that isn't always possible, but with the information provided I tried to offer some guidance that hopefully has helped, but you see that the problem is with it not being able to update anything to the server without user input, and the only input is the user closing the window, and that isn't something ASP detects directly, but with the Session variable and constant DB checks, you could hash something together that just might work.