Jump to content

Archived

This topic is now archived and is closed to further replies.

shane18

Buddy List Updater

Recommended Posts

	function BUDDY_UPDATE(){
xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = BUDDY_CHANGE;
function BUDDY_CHANGE(){
if(xmlhttp2.readyState == 4){
BUDDIES = xmlhttp2.responseText;
BUDDIES.split("#");
for(BUDDY8 in BUDDIES){
BUDDY8.split(",");
if(BUDDY8[1]){
document.getElementById(BUDDY8[0]).src="images/online.bmp";
}else{
document.getElementById(BUDDY8[0]).src="images/offline.bmp";
}
}
setTimeout(BUDDY_UPDATE(),4000);
}
}
    xmlhttp2.open("GET","update.php?ACTION=BUPDATE",true);
    xmlhttp2.send(null);
}

 

Does not work... no clue why... imma php programmer im new at javascript

 

...and yes i have <body onLoad="BUDDY_UPDATE();">

Share this post


Link to post
Share on other sites

	function BUDDY_CHANGE(){
if(xmlhttp2.readyState == 4){
BUDDIES = xmlhttp2.responseText;
BUDDIES.split("#");
for(BUDDY8 in BUDDIES){
BUDDY8.split(",");
if(BUDDY8[1]){
document.getElementById(BUDDY8[0]).src="images/online.bmp";
}else{
document.getElementById(BUDDY8[0]).src="images/offline.bmp";
}
}
setTimeout(BUDDY_UPDATE(),4000);
}
        }

function BUDDY_UPDATE(){
xmlhttp2 = new XMLHttpRequest();
xmlhttp2.onreadystatechange = BUDDY_CHANGE;
        xmlhttp2.open("GET","update.php?ACTION=BUPDATE",true);
        xmlhttp2.send(null);
}

 

Still does not work...

Share this post


Link to post
Share on other sites

What does the error console say? load ur page, clear the console, reload ur page, and post what it says the errors are. Assuming you are using chrome or ff that is, and you should be. :P

Share this post


Link to post
Share on other sites

I use Firefox :), and it says Useless setTimeout call... btw remember im new to javascript... im better at php then javascript... so im sorta clueless

Share this post


Link to post
Share on other sites

You have a fatal error here:

 

Uncaught TypeError: Cannot set property 'src' of null

 


document.getElementById(BUDDYI[0]).src="images/offline.bmp";

 

It looks like your if is failing and BUDDYI[0] is null. I'd alert out the contents of your buddy array to make sure it is being properly populated/split

Share this post


Link to post
Share on other sites

ohh im using the split wrong.... whats the javascript version of the php explode?

Share this post


Link to post
Share on other sites

implode? are you sure you wanna turn an array into a string? the php version turns {1 => '1', 2=>'2') into 12..

 

what format is your response in?

Share this post


Link to post
Share on other sites

I want to turn a string into a array...

 

bwichelt,0#Marijuana,0#nancy,0#Diamondice,0#bamdeadbird,0#VincentKelly,0#test,1

 

For example:

I want BUDDY[0] to be bwichelt,0

and BUDDY[1] to be Marijuana,0

 

... how do I do this?

Share this post


Link to post
Share on other sites

this works for me:

 


var myString="bwichelt,0#Marijuana,0#nancy,0#Diamondice,0#bamdeadbird,0#VincentKelly,0#test,1";
    
    var myArray=myString.split('#');
    
    for(var i=0; i<myArray.length; i++){
        
        alert(i+': '+myArray[i]);
        
    }//end for

Share this post


Link to post
Share on other sites

how can I write that using the for in loop? or is ur way better?

Share this post


Link to post
Share on other sites

This should be good for that chunk.. although it looks like u have more errors... like calling BUDDY_CHANGE; instead of BUDDY_CHANGE(); in BUDDY_UPDATE();...

 

anyway... this is untested..

 


var buddyString=xmlhttp2.responseText;
        var buddyArray=buddyString.split("#");
        
        for(buddy in buddyArray){
        
            var buddyOnline=buddyArray[buddy].split(',');
            
            if(buddyOnline[1]>0){// guessing 1 is online, 0 isnt
            
                alert(buddyOnline[0]+' is online!');
                //document.getElementById(buddyOnline[0]).src="images/online.bmp";
            
            }else{

                alert(buddyOnline[0]+' is not online!');            
                //document.getElementById(buddyOnline[0]).src="images/offline.bmp";
            
            }//end if
            
        }//end for

Share this post


Link to post
Share on other sites

ooohhhh you have to do VARIABLE = VARIABLE.split("x"); not just VARIABLE.split("x").... im such a noob at javascript....

 

-I didn't see your other 2 post before I posted this...

Share this post


Link to post
Share on other sites

I got it to work... but I got a question...

 

if you do

 

if(VARIABLE){

 

in javascript... 0 should = false and 1 should = true right? or can u only do that type of if in php if($TEST){

Share this post


Link to post
Share on other sites

I know what you mean, and it may work like that, but I always just do >0 or <1 in my code. Just try it out i guess. Even though it is a string, javascript may eval it as a boolean.

 

Also congrats on getting it up and running :)

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.