It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

settime out w/parms

settime out w/parms

Postby tbullard1 on Sat Apr 21, 2007 5:14 pm

Hi - I'm a long time iSeries developer and have just started playing with Ajax and I have my first dilema. I have a function - lets call it rtvjob(url,senddata,objectId)

Now - I want to have the settimeout do something like
settimeout(rtvjob(url,senddata,objectId,delay)

This does not work - It seems that the settimeout needs two strings? - I saw a post somewhere were a suggestion was to set the function to call as a var - i.e
var nextcall = function x but I still need to pass the parms - what am I missing?

thanks in advance....

tbullard
tbullard1
 
Posts: 5
Joined: Sat Apr 21, 2007 2:38 pm

Postby mwa103 on Sat Apr 21, 2007 10:54 pm

settimeout takes two parm, the function to call and the time to wait. Example: setTimeout("myFunction()",5000) waits 5 seconds then calls myFunction. I haven't tried it with variables being passes to the function, but it should work the same.

-Mike

On a side note, a good tool to have when diagnosing problems with javascript is the firebug extension in Firefox. I find it a very useful tool that I couldn't live without.
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

settime out w/parms

Postby tbullard1 on Sun Apr 22, 2007 12:43 am

Hi Mike:

I've tried all the combo's below and none work

settimeout(rtvjob(url,senddata,objectId),delay)
settimeout("rtvjob(url,senddata,objectId",delay)
settimeout('rtvjob(\'url,senddata,objectId\'),delay)

They either don't do anything -OR- in the case of the first one - it runs but does not respect the delay factor - I'll try the firefox extension you mentioned and see if I can get anyother diagnostics.

thanks again,

tbullard
tbullard1
 
Posts: 5
Joined: Sat Apr 21, 2007 2:38 pm

Postby mwa103 on Sun Apr 22, 2007 12:56 am

Javascript is case sensitive so you need to write the statement like this:
setTimeout("rtvjob(url,senddata,objectId)",delay)

Notice the quotes around the function as well, this is also a requirement. Hope this helps.

-Mike
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

settime out w/parms

Postby tbullard1 on Sun Apr 22, 2007 3:29 am

Thanks Mike - Below is the actual js I'm running: I call the script like this
<div class="click" onclick="setupRtvJobList('rtvactjob.php','Job_Status','joblist',1)"> Start Rtv Job Status</div>

The fetchdata in the RtvJobList function runs. and the setTimeout apparently runs because firebug (which I installed - thanks) gives me an error at the end of the time out function that says url is not defined though the fetchdata function used it properly
I'm probably going about this all the wrong way anyway - basically I'm just trying to rtv a set of joblog info every X amt of time and using an ajax call - write a new div element.
The type of joblog info rtv is conditioned by a select dropdown. Sorry for being so dense but I'm claiming newbie status!

<script type="text/javascript">


var theDelay = 5500;
var state = null;
var object = null;
var toRepeat = 0;


function setupRtvJobList(url,datatoSend,objectID,onOffon) {
alert(onOffon);
if (onOffon == 1) {
toRepeat = 1;
RtvJobList(url,datatoSend,objectID);
}
else toRepeat = 0;
}

function RtvJobList(url,datatoSend,objectID) {

if (toRepeat == 1) {
fetchData(url,datatoSend,objectID);
setTimeout("RtvJobList(url,datatoSend,objectID)",theDelay)
}
else{
return;
}
}

function fetchData(url,datatoSend,objectID){

var pageRequest = false
if (window.XMLHttpRequest) {
pageRequest = new XMLHttpRequest()
}
else if (window.ActiveXObject){
try {
pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e) {
try{
pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else return false
pageRequest.onreadystatechange=function() {
filterData(pageRequest,objectID)


}
var o = document.getElementById(datatoSend);
var sendData = 'sendData=' + o.options[o.selectedIndex].value;
pageRequest.open('POST',url,true);
pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
pageRequest.send(sendData);

}

function filterData(pageRequest,objectID){

if (pageRequest.readyState == 4) {
if (pageRequest.status==200)
var object = document.getElementById(objectID);
var d = document.createElement("DIV");
d.innerHTML = pageRequest.responseText;
object.appendChild(d);
}
}




</script>
tbullard1
 
Posts: 5
Joined: Sat Apr 21, 2007 2:38 pm

Postby mwa103 on Mon Apr 23, 2007 3:24 pm

Took a little while, but I finally found your problem. You need to change your setTimeout function call to this:
Code: Select all
setTimeout("RtvJobList('"+url+"','"+datatoSend+"','"+objectID+"')",theDelay)


I think it was trying to call RtvJobList passing it the text url, datatoSend, and objectID. Writing it this way will insure that the values are sent instead of the names. Be sure to add the extra ' (single quote) inside the " (double quotes). This keeps the JavaScript from breaking up your whatever.php and sending just whatever without the extension (when executed, the browser will see 'rtvactjob.php','Job_Status','joblist' instead of rtvactjob.php,Job_Status,joblist). Any questions post back here.

-Mike
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

settime out w/parms

Postby tbullard1 on Mon Apr 23, 2007 6:38 pm

I just tried this and it works - great - Using the firebug extension mentioned earlier in this thread - I was able to see that the parms were being passed as the string names - not the actual var's but I could not figure out how to correct it - Thanks so much for the help!!!

tbullard
tbullard1
 
Posts: 5
Joined: Sat Apr 21, 2007 2:38 pm

settime out w/parms

Postby tbullard1 on Mon Apr 23, 2007 7:06 pm

I also had a friend that suggested the following - and it works too...


setTimeout(function() {RtvJobList(url, datatoSend, objectID);},theDelay);

tbullard
tbullard1
 
Posts: 5
Joined: Sat Apr 21, 2007 2:38 pm


Who is online

Users browsing this forum: No registered users and 3 guests