Jump to content
Sim

jQuery round 4.. :)

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."
  }
 

}

   

Share this post


Link to post
Share on other sites
Posted (edited)

spacer.pngi wish I could use the code tags 

 

Screenshot_20200702-101256.thumb.png.958f9fe2e17f5eadd442357db0082974.png

 

Edit: my markup is correct. Phone auto corrected it.

<div id="scavageResults"></div>

Edited by Sim
  • Sad 1

Share this post


Link to post
Share on other sites

Your setTimeout call.
If memory serves correctly, you don't need to add the parenthesis for the callable function

 

setTimeout('showScavageResults', 1000);

 

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Not sure if im right but ithink you need to fill up your scavageData var inside the get function so you can use it in the function showScavageResult

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites

or if not do add the data to the function vars like this "showScavageResults(datahere)"

Share this post


Link to post
Share on other sites

Can someone make Sim his own jQuery board? 😄

  • Haha 3

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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));

 

Share this post


Link to post
Share on other sites

You are using get when you should be using getJSON.

When you return your data, use;

exit(json_encode($results));

Which then returns json, which the javascript expects, so you can access via

scavageData[0].msg

etc.

Share this post


Link to post
Share on other sites

Just a quick note: the word is "scavenge" not "scavage"

 

Sorry, but it was bothering the hell out of me.

  • Haha 1

Share this post


Link to post
Share on other sites

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);

 

Share this post


Link to post
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;
}

 

Share this post


Link to post
Share on other sites
On 7/4/2020 at 1:15 AM, gamble said:

Just a quick note: the word is "scavenge" not "scavage"

 

Sorry, but it was bothering the hell out of me.

Now that the issue is resolved. No. It's SCAVAGE. :):):):):)

[email protected] you @Magictallguy

Screenshot_20200706-020958.thumb.png.bcd15a0b6fdb751573a4ef7eb0ac0990.png

Screenshot_20200706-020951.thumb.png.41db9dfd17b52870d4afeecc1da3f689.png

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

It was a joke, sorry. Bad joke? 

After I googled, I knew I spelt it wrong. I was thinking scavenge and Savage ;).

  • Like 1

Share this post


Link to post
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?

 

Share this post


Link to post
Share on other sites
14 hours ago, gamble said:

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

Relatively certain his statement was directed to me - similar sentiment from me though

Share this post


Link to post
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...