Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setTimeOut run function w/ variable 1

Status
Not open for further replies.

snowboardr

Programmer
Joined
Feb 22, 2002
Messages
1,401
Location
PH
I am trying to get setTimeOut to run a function, with a variable in it, but its not working... I have also tried single quotes instead of quotes around the function. Any ideas?

Code:
setTimeout("saveTextBoxHeight(newheight)", 1000);

Code:
//  Make post textbox larger
function makeLarger(strTextBoxId) {
        var fieldTxt = document.getElementById(strTextBoxId);
        var newheight = fieldTxt.rows + 2;
		
	if(newheight > 40) {
		alert('Max textbox height reached.')
	} else {
		fieldTxt.rows = newheight;
		setTimeout("saveTextBoxHeight(newheight)", 1000);
	}
}

Code:
function saveTextBoxHeight(intNewHeight) {
	alert('Saved : ' + intNewHeight)
// do saving here
}

Do you got a million? www.gotamillion.com/?r=T

 
Thanks a million.

One other question what I am basicly trying to do is with the delay pause it from saving a value using AJAX, and well the time delay works (and i know how to do the ajax part), but everytime you click the button, it starts the whole function over again and thus defeats the purpose of a 5 second delay. So another words, I want the textbox row height to change, but I don't want to run that function until 5 seconds after they stop clicking. I hope that makes sense ;)

Jason
 
onclick, clear the timeout, then reset it...

Code:
var intTimeout;

clearTimeout(intTimeout);
intTimeout = setTimeout("saveTextBoxHeight(" + newheight + ")", 1000);



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
You will have to forgive me, javascript isn't my strong point. This is what I attempted off of what you told me... but its not working.

Code:
//  Make post textbox smaller
function makeSmaller(strTextBoxId) {
        var fieldTxt = document.getElementById(strTextBoxId);
        var newheight = fieldTxt.rows - 2;
        fieldTxt.rows = newheight;
		var intTimeout = 0;
		//alert(intTimeout);
		clearTimeout(intTimeout);
		
	if(newheight < 8) {
		alert('Minimum textbox height reached.')
	} else {
		fieldTxt.rows = newheight;
		if (intTimeout==0) {
			intTimeout = setTimeout("saveTextBoxHeight(" + newheight + ")", 5000);
		}
	}	
}
 
ok, i assume this function is being called when the button is clicked? we'll need to declare the timeout variable outside of the function:

Code:
var intTimeout = 0;

function makeSmaller(strTextBoxId) {
    var fieldTxt = document.getElementById(strTextBoxId);
    var newheight = fieldTxt.rows - 2;
    fieldTxt.rows = newheight;
    
    clearTimeout(intTimeout);
        
    if(newheight < 8) {
        alert('Minimum textbox height reached.')
    } else {
        fieldTxt.rows = newheight;
        intTimeout = setTimeout("saveTextBoxHeight(" + newheight + ")", 5000);
    }    
}

does this work as expected?



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top