Sim Posted July 2, 2020 Share Posted July 2, 2020 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×=" + 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." } } Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 2, 2020 Share Posted July 2, 2020 Ya know; if you used the code tags built into the forum, you'd see your markup was broken. Quote Link to comment Share on other sites More sharing options...
Sim Posted July 2, 2020 Author Share Posted July 2, 2020 (edited) i wish I could use the code tags Edit: my markup is correct. Phone auto corrected it. <div id="scavageResults"></div> Edited July 2, 2020 by Sim 1 Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 2, 2020 Share Posted July 2, 2020 Your setTimeout call. If memory serves correctly, you don't need to add the parenthesis for the callable function setTimeout('showScavageResults', 1000); Quote Link to comment Share on other sites More sharing options...
Sim Posted July 3, 2020 Author Share Posted July 3, 2020 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×=" + 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. Quote Link to comment Share on other sites More sharing options...
ags_cs4 Posted July 3, 2020 Share Posted July 3, 2020 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 Quote Link to comment Share on other sites More sharing options...
Sim Posted July 3, 2020 Author Share Posted July 3, 2020 (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: Edited July 3, 2020 by Sim Quote Link to comment Share on other sites More sharing options...
ags_cs4 Posted July 3, 2020 Share Posted July 3, 2020 or if not do add the data to the function vars like this "showScavageResults(datahere)" Quote Link to comment Share on other sites More sharing options...
SRB Posted July 3, 2020 Share Posted July 3, 2020 Can someone make Sim his own jQuery board? 😄 3 Quote Link to comment Share on other sites More sharing options...
Sim Posted July 3, 2020 Author Share Posted July 3, 2020 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); }); Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 3, 2020 Share Posted July 3, 2020 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? Quote Link to comment Share on other sites More sharing options...
Sim Posted July 3, 2020 Author Share Posted July 3, 2020 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)); Quote Link to comment Share on other sites More sharing options...
SRB Posted July 4, 2020 Share Posted July 4, 2020 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. Quote Link to comment Share on other sites More sharing options...
gamble Posted July 4, 2020 Share Posted July 4, 2020 Just a quick note: the word is "scavenge" not "scavage" Sorry, but it was bothering the hell out of me. 1 Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 4, 2020 Share Posted July 4, 2020 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); Quote Link to comment Share on other sites More sharing options...
Sim Posted July 5, 2020 Author Share Posted July 5, 2020 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×=" + 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; } Quote Link to comment Share on other sites More sharing options...
Sim Posted July 6, 2020 Author Share Posted July 6, 2020 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. :):):):):) Lol@ you @Magictallguy Quote Link to comment Share on other sites More sharing options...
gamble Posted July 6, 2020 Share Posted July 6, 2020 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. Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 6, 2020 Share Posted July 6, 2020 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 Quote Link to comment Share on other sites More sharing options...
Sim Posted July 6, 2020 Author Share Posted July 6, 2020 It was a joke, sorry. Bad joke? After I googled, I knew I spelt it wrong. I was thinking scavenge and Savage ;). 1 Quote Link to comment Share on other sites More sharing options...
gamble Posted July 6, 2020 Share Posted July 6, 2020 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? Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted July 7, 2020 Share Posted July 7, 2020 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.