Jump to content
MakeWebGames

jQuery round 4.. :)


Sim

Recommended Posts

I load my page with some of my previous jQuery code. The new page I load some new HTML:

Partial HTML:

<div I'd="scavageResults"></div>

 

The JavaScript that is loaded is:

var scavageTimer;
var counter;
var scavageData;
var scavageCounter;

function performScavage(){
 
  //e.preventDefault();
  
  var timez = jq("#scavageTimes").val();
  
  var url = "?page=scavage&times=" + timez;
  
   alert(timez);
  
  if(timez <= 0) {
    alert("You have to search the streets at least one time!");
    return false;
  }

  jq.get(url, function(scavageData) {
    alert(scavageData);
    scavageCounter = 0;
    alert(1);
    setTimeout('showScavageResults()', 1000);      

  });
}

function showScavageResults(){
  alert(2);
  jq("#scavageResults").append(scavageData[scavageCounter].msg);
  scavageCounter++;
}

 

The problem is it never appends my scavageData. I think it's not showScavageResults. All my other alerts get down.

 

scavageData returned is:

 

Quote
  array(4) {
  [0]=>
  array(3) {
  ["reward"]=>
  string(6) "Points"
  ["amount"]=>
  int(5)
  ["msg"]=>
  string(32) "In the alley to noticed 5 Points"
  }
  [1]=>
  array(2) {
  ["reward"]=>
  bool(false)
  ["msg"]=>
  string(80) "I get tired of walkemp past empty candy bar rappers, I should get a Butterfinger"
  }
  [2]=>
  array(2) {
  ["reward"]=>
  bool(false)
  ["msg"]=>
  string(30) "Hydros, hydros, i like hydros."
  }
  [3]=>
  array(3) {
  ["reward"]=>
  string(6) "Points"
  ["amount"]=>
  int(6)
  ["msg"]=>
  string(63) "While following someone walking there dog you noticed 6 Points."
  }
 

}

   
Link to comment
Share on other sites

20 hours ago, Magictallguy said:

Ya know; if you used the code tags built into the forum, you'd see your markup was broken

The code is now this:

var scavageTimer;
var counter;
var scavageData;
var scavageCounter;

function performScavage(){
 
  //e.preventDefault();
  
  var timez = jq("#scavageTimes").val();
  
  var url = "?page=scavage&times=" + timez;
  
   alert(timez);
  
  if(timez <= 0) {
    alert("You have to search the streets at least one time!");
    return false;
  }

  jq.get(url, function(scavageData) {
    alert(scavageData);
    scavageCounter = 0;
    alert(1);
    setTimeout('showScavageResults', 1000);      
    alert(3);
  });
}

function showScavageResults(){
  alert(2);
  jq("#scavageResults").append(scavageData[scavageCounter].msg);
  scavageCounter++;
}

 

I receive the alerts 1 and 3. So the settimeout doesn't error. But it's not executing the code withing function at all. 

Link to comment
Share on other sites

I have thought if that. Let me try that. I would still think it would still show my alert before using the scavageData though.

Edit: I'm one are close to getting it was. I can call the functions now. It seems like the jQuery Ajax call must completed before it can call after function with timeout.

The problem now it's I can't access the data for some reason. I done tried every variation of the array.  I commented the outcome of the alert next to the scavageData alerts.

Quote


  jq.get(url)
    .always( function(data){
      scavageData = data;
      scavageCounter = 0;
      alert(1); //1
      alert(scavageData[0][0]); //a
      alert(scavageData[0]["msg"]);//undefined
      alert(scavageData[0].msg); //undefined
     alert(scavageData);//disolays screenshot
      setTimeout(showScavageResults(), 1000);      
      alert(3);
  });
}

 

 

A snipplet of my PHP, just to show how array is created.

$results[] = array(
          "reward" => false,
          "msg" =>$this->fail[mt_rand(0, count($this->fail))]
          );
      }
    }

    die(var_dump($results));
  }  

scavageData:

Screenshot_20200703-071948.thumb.png.daecc4ae3347a259a24f097f88d08b5c.png

Edited by Sim
Link to comment
Share on other sites

The scavageData is shown in alert now from previous post code:

Here's the alert data: like the code comments say

I can't get the days from my array

jq.get(url)
    .always( function(data){
      scavageData = data;
      scavageCounter = 0;
      alert(1); //1
      alert(scavageData[0][0]); //a
      alert(scavageData[0]["msg"]);//undefined
      alert(scavageData[0].msg); //undefined
     alert(scavageData);//disolays screenshot
      setTimeout(showScavageResults(), 1000);      
      alert(3);
  });

spacer.pngScreenshot_20200703-101237.thumb.png.b1717eba40fd01592dedbabdf9b75e2a.png

Link to comment
Share on other sites

If I'm reading that right, you should be able to access the `amount` key (presumably repurposed to a context of "days" if applicable) like a standard array.
 

data[0].amount

Edit; which I see you've tried.

In what format is `data`? Is it a JSON string, or something else?

Link to comment
Share on other sites

Edit: I think I need to remove var_dump. LoL... I just now seen this. I will try and post results.

 

Here's a partial of the PHP code just to see what is returned:

        $results[] = array(
            "reward" => false,
            "msg" => $this->fail[mt_rand(0, count($this->fail))]
        );
    }
}
die(var_dump($results));

 

Link to comment
Share on other sites

On 7/4/2020 at 1:33 PM, Magictallguy said:

To add on to SRB's statement; in some setups, it may be necessary to parse the JSON string into an object before JS will let you use it as such.

let scavengeData = JSON.parse(data); alert(scavengeData[0].amount);


let scavengeData = JSON.parse(data);
alert(scavengeData[0].amount);

 

That's how I fixed the issue. It looks like I forgot to post the answer.
 

var scavageTimer;
var counter;
var scavageData;
var scavageCounter;

function performScavage(){
 
  //e.preventDefault();
  
  var timez = jq("#scavageTimes").val();
  
  var url = "?page=scavage&times=" + timez;
  
  
  if(timez <= 0) {
    alert("You have to search the streets at least one time!");
    return false;
  }

  jq.get(url)
    .always( function(data){
      
      scavageData = JSON.parse(data);

      scavageCounter = 0;
      scavageTimer = setInterval(showScavageResults, 1000);      
  });
}

function showScavageResults(){
 
  jq("#scavageResults").append(scavageData[scavageCounter].msg + "<br>");
  
  if(scavageCounter == scavageData.length){
    clearInterval(scavageTimer);
    
  }
  
  scavageCounter = scavageCounter + 1;
}

 

Link to comment
Share on other sites

Yeah...I don't think you read that definition that you presented. Scavage has nothing to do with searching, which this mod is clearly aimed towards: "You have to search the streets at least one times!"

 

Scavenge means to search for discarded items.

Link to comment
Share on other sites

Lol @ you @Sim
Not only the wrong word for the intended meaning, but a word from Old and Middle English; suggesting it's simply not in use anymore.
That's the trouble with using older language - contexts are different.

How about stop being a young me and drop the attitude; it ain't gonna help you in this world

Link to comment
Share on other sites

My mistake for misinterpreting the message. I didn't interpret it as a joke, so my bad.

 

On a positive note, I am glad you got your issue all figured out! Have a great day!

8 hours ago, Sim said:

It was a joke, sorry. Bad joke?

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...