/    Sign up×
Community /Pin to ProfileBookmark

Time interval problem….

I am trying to fix the code ….let me explain and paste the code here:
I have a textarea, and textbox. When i start typing something in the textarea, the time in the textbox incerases by 1 sec and stops at 5 second if user stop typing…again when user start typing it goes next 5 seconds…..as long as the user types the time goes on…..
What i need to do: ex: I have a textbox (which has 100 as value let us say) when the user types in the textarea count in the textbox goes for 101,102,103,104,105….if the user stops, if NOT it goes along)…I have a code it works if the user starts fresh, But NOT it takes from a textbox already has some value in it (let us say 100 in the above case)
Here is the code which works for a fresh start with empty textbox, my need is to increase the time if something has already in it (ex 100), and the user types some thing in the textarea the time increases by 5 secs…say 101,102,…..

[CODE]
<script type=”text/javascript”>
var seconds = 0,
timer = null,
timestop = null;

function increaseSeconds() {
++seconds;
document.getElementById(“timer”).value = seconds;
}

function startTimer() {
timer = setInterval(increaseSeconds, 1000);
}

function stopTimer() {
clearInterval(timer);
timer = null;
}

function descriptionKeyUp() {
if (timer === null) {

startTimer();
}
else {
clearTimeout(timestop);
}

timestop = setTimeout(stopTimer, 6000);
}
</script>
PHP/HTML CODE:

html:

<form>
<tr>
<th scope=”row”><textarea name=”taskdesc” cols=”45″ rows=”10″ id=”taskdesc” onkey onkeyup=”descriptionKeyUp()”></textarea></th>
</tr>

<p>typing time:
<input type=”input” name=”seconds” id=”timer” value=”0″ />
<input type=”submit” id=”submit” value=”Submit” />

</p>
</form>

[/CODE]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@TcobbJan 21.2011 — I don't understand what the problem is. I took and pasted your code and HTML as is and it seems to perform as you have described it as wanting to perform. As long as I keep typing the count continues and when I stop it continues to count for 5 seconds. If the count stops at 101, for example, it does indeed start from that point when I start typing again.

What is it that you want it to do?
Copy linkTweet thisAlerts:
@desiinusa123authorJan 21.2011 — I don't understand what the problem is. I took and pasted your code and HTML as is and it seems to perform as you have described it as wanting to perform. As long as I keep typing the count continues and when I stop it continues to count for 5 seconds. If the count stops at 101, for example, it does indeed start from that point when I start typing again.

What is it that you want it to do?[/QUOTE]


Ok when you first start there is nothing in txtbox....because the timer is set to zero. imagine this as a user first time entering the time into Database. later on he wanted to edit. Let us say when the user submitted form, the time is 101, when the user tries to edit the page it should bring back 101...this works for me...but it should increase time 101, 102, 103, 104, 105, 106 (5 seconds) when the user starts typing......the problem is it works fine when the user starts with zero...and continues ....But I want to start with some other number, not a constant( but this number should be taken from the textbox and do the same thing instead of starting from zero)
Copy linkTweet thisAlerts:
@TcobbJan 21.2011 — So--correct me if I'm wrong---You want the last value of the counter to be the starting value after the form is submitted? What happens after the form is submitted? Does it in effect just return the same page?

If this is the case you might want to use AJAX to send the data to the server rather than a standard form submission which causes a new page to be loaded.
Copy linkTweet thisAlerts:
@desiinusa123authorJan 21.2011 — So--correct me if I'm wrong---You want the last value of the counter to be the starting value after the form is submitted? What happens after the form is submitted? Does it in effect just return the same page?

If this is the case you might want to use AJAX to send the data to the server rather than a standard form submission which causes a new page to be loaded.[/QUOTE]

I can get back the last value of the counter as the starting value when i click update/edit-This works....All i need is 5 sec interval should be working....even I am able to make it work like an infinite loop....means as the above example it starts from 101,....goes on but never stops...All I need is when i start typing in the textarea, it picks up from the textbox(wahat ever the value for ex:101) and goes on 102,103,104,105,106...if the user stops..else it goes until the user stops, when the user stop the time counter stops with 5 sec interval...Hope you got it...! which is nothing but it should work the same as the code i pasted BUT start from by taking the value from the textbox and do the same logic (5 secs increments while user types)
Copy linkTweet thisAlerts:
@thraddashJan 21.2011 — This was an interesting puzzle to solve...

Hope you don't mind but I got rid of all those useless global variables and most of the functions.

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>5 Second Increments</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">

function descriptionKeyUp()
{
var
$timer = document.getElementById('timer'),
$counter = 0;

if (!$timer.intervalID) {
$timer.intervalID = setInterval(function()
{
if (++$counter >= 5) {
clearInterval($timer.intervalID);
$timer.intervalID = 0;
}
$timer.value = parseInt(0 + $timer.value, 10) + 1;
}, 1000);
}
}

</script>

</head>
<body>

PHP/HTML CODE:

html:
<form>
<table>
<tr>
<th scope="row"><textarea name="taskdesc" cols="45" rows="10" id="taskdesc" onkeyup="descriptionKeyUp();"></textarea></th>
</tr>
</table>

<p>
typing time:
<input type="text" name="seconds" id="timer" value="0" />
<input type="submit" id="submit" value="Submit" />
</p>
</form>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@thraddashJan 22.2011 — Everytime I read the requirement I think of something different.

So here is just a modification to the descriptionKeyUp function that stops counting 5 seconds after the user stops typing (if that's what was really asked for):

[CODE]function descriptionKeyUp()
{
var $timer = document.getElementById('timer');

$timer.counter = 5;
if (!$timer.intervalID) {
$timer.intervalID = setInterval(function()
{
if (!--$timer.counter) {
clearInterval($timer.intervalID);
$timer.intervalID = 0;
}
$timer.value = parseInt(0 + $timer.value, 10) + 1;
}, 1000);
}
}[/CODE]
Copy linkTweet thisAlerts:
@tirnaJan 22.2011 — I'm not totally sure what you need, but this stops the clock at the next multiple of 5 secs after the user stops typing. The start time (set to 100) is whatever the default value is in the textbox.

[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var timer=0; time=0;
window.onload=function(){
txtA = document.getElementById('taskdesc');
txtTimer = document.getElementById('txtTime');
startTime = new Number(txtTimer.value);
txtA.onkeyup = releaseTimer;
}
function releaseTimer(){
if(timer == 0){
timer = setInterval(incrTimer,1000);
}
}
function incrTimer(){
txtTimer.value = ++time + startTime;
if(time%5 == 0){
clearInterval(timer);
timer = 0;
}
}
</script>
</head>
<body>
<div>
<p>
PHP/HTML CODE:html:
</p>
<textarea name="taskdesc" cols="45" rows="10" id="taskdesc"></textarea>
</div>
<p>
typing time:
<input type="text" name="seconds" id="txtTime" value="100" />
<input type="submit" id="submit" value="Submit" />
</p>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@desiinusa123authorJan 22.2011 — I'm not totally sure what you need, but this stops the clock at the next multiple of 5 secs after the user stops typing. The start time (set to 100) is whatever the default value is in the textbox.

[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var timer=0; time=0;
window.onload=function(){
txtA = document.getElementById('taskdesc');
txtTimer = document.getElementById('txtTime');
startTime = new Number(txtTimer.value);
txtA.onkeyup = releaseTimer;
}
function releaseTimer(){
if(timer == 0){
timer = setInterval(incrTimer,1000);
}
}
function incrTimer(){
txtTimer.value = ++time + startTime;
if(time%5 == 0){
clearInterval(timer);
timer = 0;
}
}
</script>
</head>
<body>
<div>
<p>
PHP/HTML CODE:html:
</p>
<textarea name="taskdesc" cols="45" rows="10" id="taskdesc"></textarea>
</div>
<p>
typing time:
<input type="text" name="seconds" id="txtTime" value="100" />
<input type="submit" id="submit" value="Submit" />
</p>
</body>
</html>
[/CODE]
[/QUOTE]


Works like a charm....!!!so cool I am really thankful 2 u...
Copy linkTweet thisAlerts:
@tirnaJan 22.2011 — no problem ?
×

Success!

Help @desiinusa123 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.1,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...