/    Sign up×
Community /Pin to ProfileBookmark

I’m done racking my brain by myself…need A LOT of help!

Hello everyone,

I hope this solution comes a lot easier for you.

It’s a little difficult to explain, but I will do my best. Please feel free to ask about anything that is not clear.

On 10 pages of a web-like presentation, I have a function that checks the amount of time a viewer has spent on a page against a predefined time value (i.e. 3 minutes) when the viewer is trying to advance to the next page.

If the viewer has not spent at least the amount of time specified by the predefined time value when trying to advance to the next page, the viewer is notified with the following statement:

“You’re reading awfully fast. Don’t you think you should take it a bit more slowly?”

However, within those 10 pages are links to photos, .pdf documents, and references. The links to these items are not restricted by the page’s timer.

I need to keep track of the time a viewer has spent on a page, even after accessing material from an unrestricted link within the timed page.

Here is an example:

You access page 2 of the presentation which has a minimum time restriction of 3 minutes (which you don’t know of course). A quarter-way down the page, you follow a link to view some photos and before accessing the photos you had spent 45 seconds on the page. You now have 2 minutes and 15 seconds remaining on the timed page.

When you return to the timed page from the photos, you continue reading. Three quarters down the page, you follow a link to the “Notes” page where references are listed. Before accessing the “Notes” page you had spent 2 minutes and 15 seconds total on the page. You now have 45 seconds remaining on the timed page.

How would I use cookies to retain the value of “TimeSpent” and use that value when the viewer returns to the timed page from a link which is not restricted by the timer?

I have the following cookie, but I don’t think it works the way I would like it to and this is because when the viewer returns to the time-restricted page, the function “checkspeed” will only test the value of the last recorded value for “TimeSpent” as opposed to the value of the TOTAL time the viewer has spent on the page.

Take a look:

<html>
<head>
<title>Timed Page Cookie TEST</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<script language=”JavaScript” type=”text/javascript”>
<!–

TimeIn = new Date();

function SetCookie() {
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2
var cookieName=”PageTimer”;
var TimeSpent=(TimeOut – TimeIn)
var cookieValue=TimeSpent;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+”=”+escape(cookieValue)+ “;expires=”+expire.toGMTString();
return (TimeSpent);
}

function ReadCookie() {
var NameOfCookie=”PageTimer”;
if(document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+”=”);
if(begin != -1)
{
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(“;”,begin);
if(end == -1) end = document.cookie.length;
TimeSpent=(document.cookie.substring(begin,end));
}
}
}

function checkspeed() {
ReadCookie(PageTimer);
TimeToExit= new Date();
difference = TimeToExit – TimeSpent;
if (difference <= 60000 ){
alert(“You’re reading awfully fast. Don’t you think you should take it a bit more slowly?”);
return false;
}
else {
return true;
}
}

//–>
</script>
</head>

<body onLoad=”SetCookie();”>
This link will allow users to view <a href=”photos.htm” onClick=”SetCookie();”>refernced material</a>. <br>
This link goes to the next page and is timed. <a href=”page3.htm” onClick=”return checkspeed();”>Next
Page</a>
</body>
</html>

What do I need to do??

Thank you.

~Darron

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@AdamGundryApr 27.2003 — I don't understand why you want to do this, but...

You could presumably cookie the time the user enters the page (via onLoad), then test the difference when they leave via the next page hyperlink (in checkspeed()), therefore not needing to check how long they spend on other pages.

Adam
Copy linkTweet thisAlerts:
@dmason165authorApr 27.2003 — It doesn't matter how long they spend on the other pages within a timed page. What does matter is how long they spend on the actual timed page.

The purpose of the cookie is to retain the amount of time they've spent on the page while the user visits other pages within the timed page, so that when they return to the timed page from the other pages, the checkspeed timer doesn't start all over.

I hope that clarifies. Post with more questions if you need to.

~D
Copy linkTweet thisAlerts:
@dmason165authorApr 27.2003 — Ok everyone, I think a may have found a way I can do what I want. But, I will need some answers from you.

First, is it possible to set a cookie with a predefined value of time (i.e. 180000)??

If so, then I can set the cookie with the minimal amount of time a viewer must spend on a page (i.e. 180000 or 3 minutes).

The beginning of the code would look something like this:

<script language="JavaScript" type="text/javascript">

<!--

TimeIn = new Date();

function SetCookie() {

var TimeOut = new Date();

var today = new Date();

var expire = new Date();

var nDays=2

var cookieName="PageTimer";

var TimeSpent=(TimeOut - TimeIn)

var TimeRemaining=([COLOR=red]180000[/COLOR] - TimeSpent)

var cookieValue=TimeRemaining;

expire.setTime(today.getTime() + 3600000*24*nDays);

document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();

}


The only thing is I don't want to actually put "180000" into the equation, but the value of the cookie, because the value of the cookie will continue to change.

Then, when the cookie is read, it would do something like:

if (TimeRemaining > 0) {

alert("You're reading awfully fast. Don't you think you should take it a bit more slowly?");

return false;

}

else {

return true

}

Wouldn't this work??
Copy linkTweet thisAlerts:
@dmason165authorApr 27.2003 — I don't know if this is what's considered "double posting" but if it is, no harm intended.

Anyway, I think this might be the final code, if someone could look over it and tell me if it would work or not. And if not, what may need to be changed.

<html>

<head>

<title>Timed Page Cookie TEST</title>

<script language="JavaScript" type="text/javascript">

<!--

TimeIn = new Date();

function SetCookie() {

var TimeOut = new Date();

var today = new Date();

var expire = new Date();

var nDays=2;

var cookieName="TimeRemaining";

var TimeSpent=(TimeOut - TimeIn);

var TimeRemaining=(TimeRemaining + TimeSpent);

var cookieValue=TimeRemaining;

expire.setTime(today.getTime() + 600000);//for testing purposes (will be 2 days)

document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();

}

function ReadCookie() {

var NameOfCookie="TimeRemaining";

if(document.cookie.length > 0)

{

begin = document.cookie.indexOf(NameOfCookie+"=");

if(begin != -1)

{

begin += NameOfCookie.length + 1;

end = document.cookie.indexOf(";",begin);

if(end == -1) end = document.cookie.length;

TimeSpent=(document.cookie.substring(begin,end));

}

}

}

function checkspeed() {

SetCookie();

ReadCookie(TimeRemaining);

if (TimeSpent < 180000 ){

alert("You're reading awfully fast. Don't you think you should take it a bit more slowly?");

return false;

}

else {

return true;

}

}

//-->

</script>

</head>

<body>

This link will allow users to view <a href="photos.htm" onClick="SetCookie();">refernced material</a>. <br>

This link goes to the next page and is timed. <a href="page3.htm" onClick="return checkspeed();">Next

Page</a>

</body>

</html>


What I have done differently in this code is, instead of having the value of the cookie "TimeRemaining" predefined, I just added the value of TimeSpent to TimeRemaining and when the cookie is read, the checkspeed function compares the value of TimeRemaining (which is now called TimeSpent) against the least amount of time the user should spend on the page.

Take a look at the first line under the checkspeed function. I am trying to make it so the cookie is set when the user is leaving so the last value of "TimeRemaining" can be recorded and applied to the equation. Or, should I just add SetCookie() to the onClick method for the "Next Page" link. That would work too wouldn't it?

Thanks for your help!

~D
Copy linkTweet thisAlerts:
@dmason165authorApr 28.2003 — Dave,

Yes, that's what I was referring to. Thanks for the info!

So what did you think about my cookie script...did it look right to you.

I thought it looked right but it didn't work when I tried it.

~D
Copy linkTweet thisAlerts:
@dmason165authorApr 28.2003 — Hello everyone,

Making some good progress figuring out the answer to my question to Dave above...I'll post the resons in a later post FYI.

However, I do need an answer relevant to this problem, but I will post the question as a seperate thread so people won't get confused with the posts in this thread and so that if someone searches on a similar question, the answer won't be difficult to find.

~D
×

Success!

Help @dmason165 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...