setTimeout, clearTimout problems

S

Seansan

Hi, am having some troubles with timeouts. The code starts at
verifyform. I was debugging and tested hitting the form submit twice
(within 3 seconds)!

In IE6 I get an error saying that I have an error when I set timerID =
0; In Firefox the form submits the second click which it shouldnt do as
all values are still empty (within the 3 seconds).

Any ideas?

kind regards, seansan


<CODE STARTS HERE>

var timerID = 0;
var msgtime = 3000;

// Define empty function
function empty(varname)
{
return(varname == '');
}

// Verify form contents
function verifyform(frm)
{
var _user = frm._user.value;
var _pass = frm._pass.value;
if (empty(_user) || empty(_pass))
{
set_message('message', 'Please enter your username and a password');
return false;
} else {
return true;
}
}

function clear_message(lbl)
{
document.getElementById(lbl).innerHTML = ' ';
}

// Set error message text
function set_message(lbl, txt)
{
if (timerID) {
clearTimout(timerID);
timerID = 0;
}
document.getElementById(lbl).innerHTML = txt;
var clear_message = "clear_message('" + lbl + "');";
timerID = window.setTimeout(clear_message, msgtime);
}
 
E

Evertjan.

Seansan wrote on 11 mrt 2007 in comp.lang.javascript:
if (timerID) {
clearTimout(timerID);
timerID = 0;
}

Just:

clearTimeout(timerID);

will work fine, as long as you spell it correctly.

clearTimout/clearTimeout
 
S

scripts.contact

Seansan said:
In IE6 I get an error saying that I have an error when I set timerID =
0; In Firefox the form submits the second click which it shouldnt do as
all values are still empty (within the 3 seconds).
function set_message(lbl, txt)
{
if (timerID) {
clearTimout(timerID);
clearTimeout(timerID)

timerID = 0;
}
document.getElementById(lbl).innerHTML = txt;
var clear_message = "clear_message('" + lbl + "');";
timerID = window.setTimeout(clear_message, msgtime);
}

using string is just as bad as eval method. Use function -

timerID= setTimeout(function(){ clear_message(lbl) } ,msgtime)
 
S

scripts.contact

Seansan said:
Hi, am having some troubles with timeouts. The code starts at
verifyform. I was debugging and tested hitting the form submit twice
(within 3 seconds)!

In IE6 I get an error saying that I have an error when I set timerID =
0; In Firefox the form submits the second click which it shouldnt do as
all values are still empty (within the 3 seconds).

Any ideas?

kind regards, seansan


<CODE STARTS HERE>

var timerID = 0;
var msgtime = 3000;

// Define empty function
function empty(varname)
{
return(varname == '');
}

// Verify form contents
function verifyform(frm)
{
var _user = frm._user.value;
var _pass = frm._pass.value;
if (empty(_user) || empty(_pass))
{
set_message('message', 'Please enter your username and a password');
return false;
} else {
return true;
}
}

function clear_message(lbl)
{
document.getElementById(lbl).innerHTML = ' ';
}

// Set error message text
function set_message(lbl, txt)
{
if (timerID) {
clearTimout(timerID);
timerID = 0;
}
document.getElementById(lbl).innerHTML = txt;
var clear_message = "clear_message('" + lbl + "');";
timerID = window.setTimeout(clear_message, msgtime);
}
 
S

scripts.contact

....

using string is just as bad as eval method. Use function -

timerID= setTimeout(function(){ clear_message(lbl) } ,msgtime)

timerID = setTimeout(function(){ clear_message('lbl') } ,msgtime)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top