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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function executes the wrong way

Status
Not open for further replies.

stevecrozz

IS-IT--Management
Jul 4, 2005
20
US
here's my function. After the closing </body> tag I call it. I want it to call insert which loads a page ajax style, and then nap for 2 seconds. The behavior I get is a nap for 2 seconds, and then the page loads.

function rotate(link_num, containerid, clicked) {
insert(link_num, containerid)
nap (2000)
}
 
function insert(link_num, containerid){
var url = extern_links[link_num-1];

var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
// if (bustcachevar) {//if bust caching of external page
// bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
// }
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}


function nap (m) {
var then = new Date(new Date().getTime() + m)
while (new Date() < then) {
}
}
 
I can comment out the entire contents of insert leaving just
insert () {
}
, and I get the same delay before the page will load anything.
 
[1] I don't think nap(m)'s construction is good. I would scrap it. Use setTimeout() function. The question is how to implement it. I would come to it.
[2] Extend the scope of page_request to be global, so as to accommodate to be called by setTimeout().
[3] You use asyn being true. In your onreadystatechange handler, you have no measure to assure the loading the complete (readyState==4) and/or the http status being OK (200). To get a quick fix add a few line on testing readyState. (And I take for granted the status be 200.)
[4] I assume the proper functioning of loadpage function..
[tt]
[blue]var page_request;[/blue]
function insert(link_num, containerid[blue],m[/blue]){
var url = extern_links[link_num-1];

[blue]page_request = false[/blue]
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
[blue]if (page_request.readyState == 4) {
setTimeout("loadpage(page_request,"+containerid+")",m)
}[/blue]
}
// if (bustcachevar) {//if bust caching of external page
// bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
// }
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}
[/tt]
[green](I edit the whole thing in the textarea. I hope you get the idea behind the change. I might not have changed all what need change to be consistent. So to have to be pro-active in adapting some of the ideas in this.)[/green]

The function rotate() would call insert like this accordingly.
[tt]
function rotate(link_num, containerid, clicked) {
insert(link_num, containerid[blue],2000[/blue])
//nap (2000)
}
[/tt]
 
thanks so much, that was extremely helpful. I'm wondering since the page won't finish loading until page_request.open() is called, your added lines:

if (page_request.readyState == 4) {
setTimeout("loadpage(page_request,"+containerid+")",m)
}

should go after that line? I'm going to give it a run right now.
 
Well your mods do make this work the way it stands, but the functionality I really want is for this insert function to auto rotate. So my first idea was to call it recursively at the end. In my mind it seems like the function should run, then call itself unless the user calls the function by clicking a link, passing a value of true into 'clicked' variable.

I get error out of memory at line 25 after maybe 10-15 seconds in IE, and I get a similar response in firefox.

var page_request = false
function insert(link_num, containerid, clicked){
var url = extern_links[link_num-1];

var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE //LINE 25
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
// if (bustcachevar) {//if bust caching of external page
// bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
// }
page_request.open('GET', url+bustcacheparameter, true)
if (page_request.readyState == 4) {
setTimeout("loadpage(page_request,"+containerid+")",2000)
}

page_request.send(null)
if(!clicked)
if (link_num < 500)
insert(link_num%extern_links.length, containerid, clicked)

}
 
Alright I have it working now, here's the resulting code if anyone would like to see it. Just so everyone knows, most of this code has been borrowed from various other forums and is only partially my own.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<SCRIPT LANGUAGE='JavaScript'>
<!--

// ***********************************************
// AUTHOR: LLC
// URL: // Use the script, just leave this message intact.
// Download your FREE CGI/Perl Scripts today!
// ( )
// ***********************************************

var changeRate = 2000; // 1000 = 1 second
var linkNumber = 0;
var bustcachevar=0 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="var bustcacheparameter=""
var extern_links = new Array(3)
extern_links[0] = "external.htm";
extern_links[1] = "external2.htm";
extern_links[2] = "external3.htm";

function changeStatus() {
if (linkNumber == extern_links.length) {
linkNumber = 0;
}
linkNumber++;
insert(linkNumber, 'top')
setTimeout("changeStatus();",changeRate);
}

function insert(link_num, containerid){
var url = extern_links[link_num-1];

var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

changeStatus(); // leave here to run right away

// -->
</SCRIPT>
<style type="text/css">
body {
margin:0px;
}
#top {
position:relative;
height:80%;
width:100%;
background-color: #ffffff;
}
#bottom {
position:relative;
height:20%;
width:100%;
padding-top:5px;
background-color: #ffffff;
}
</style>
</head>
<body>
<div id="top">

</div>
<div id="bottom">
<a href="javascript:insert(1, 'top');">external</a>
<a href="javascript:insert(2, 'top');">external1</a>
<a href="javascript:insert(3, 'top');">external2</a>
</div>
</body>
</html>
 
Glad you get your page work.

Upon re-reading what I posted, I just want to amend a line (mine and my fault) to make it proper.
[tt] setTimeout("loadpage(page_request,[red]'[/red]"+containerid+"[red]'[/red])",m)[/tt]
Please do not be disturbed by this amendment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top