the sandman
the sandman

Reputation: 1029

How can i automatically have a page save the current time to the DB in 5 minute intervals w/o postback?

Basically i have a C# Web App where a user views a streaming video. While they are viewing it, i have a timer running in the code behind. When they finish viewing the video, they hit a submit button which takes the difference from the start time and end time and emails the total viewing time to me.

The issue is, if they lose their session or their internet connection drops out for a brief moment, the time is lost and i have no proof how long they viewed the 1.5 hour video for.

I was wondering where to start to have it automatically save the time to the database like every 5 minutes behind the scenes without it affecting the streaming video or posting back, that way they have record of how much viewing time there was up until they lost connection.

Upvotes: 2

Views: 306

Answers (5)

Greg Bala
Greg Bala

Reputation: 3903

Suggestions to use JQuery or other AJAX library to make ajax call every 5 min are good but if you are new to this, you can use the asp.net ajax extensions Timer object (Toolbox->AJAX Extensions->Timer). its not hard to use, it follows a typical event driven model like other ASP.NET controls

Upvotes: 0

Andy M.
Andy M.

Reputation: 491

You say "without a postback" - but does that mean a whole page postback? Otherwise it should be pretty easy to make an AJAX call back to the server from some JavaScript. You could use the JQuery library to easily make the call. Here's a pretty good page discussing how to call a page method from JQuery: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Upvotes: 0

bugfixr
bugfixr

Reputation: 8077

One option would be to setup an ajax call to talk to the server on an interval to say "I'm still here" - if your ajax call sends a video ID or some other type of session identifier, it can update your database with the appropriate time.

I'd suggest looking into using jQuery for this along with [WebMethods]. It makes it quite simple to accomplish. Here's a nicely written article that talks about how to accomplish it with asp.net: http://dotnetslackers.com/articles/ajax/Using-jQuery-with-ASP-NET.aspx

Upvotes: 0

David
David

Reputation: 73574

We used the information from this blog post to accomplish it in our apps. There are code samples that show exactly how to do it. Adjust the

window.setInterval('SaveUserInput()', 10000);

line as needed to change the interval.

Upvotes: 4

jrummell
jrummell

Reputation: 43087

You could store the start time in a hidden field when page loads, and then use javascript to post to a webservice at a set interval with the start time.

Upvotes: 1

Related Questions