javascript - How to add a delay between each getjson call? -


i calling getjson inside loop. code prints items doesnt ommit items not on sale!

this part checks if item on sale or not:

 $.getjson('http://anyorigin.com/get?url=http://www.asite.com/itemtocheck.php='+ itemname + '/&callback=?', function(data){         var sitecontents = data.contents;            var n=sitecontents.search("this item not on sale");      if(n=-1)         {          //alert("this item on sale. n:"+n);           var sitecontents2 = "this item on sale:"+itemname;          document.getelementbyid("mydiv").innerhtml += sitecontents2;         }; 

i dont know problem if statement comes true if item not on sale! there way put delay between each call getjson inside "for loop"(very short delay getjson fetch data , have enough time filter it)?

i not sure think maybe "for loop" calling getjson fast not giving enough time each item checked. tried doing this:

settimeout(getjsonresult('pen'), 500); 

but reason didnt work! 1 fix code filters out items not on sale ?thanks

    <script>    function getjsonresult(itemname) { //alert("test:"+itemname);  $.getjson('http://anyorigin.com/get?url=http://www.asite.com/itemtocheck.php='+ itemname + '/&callback=?', function(data){         var sitecontents = data.contents;         //writes textarea     document.myform.outputtext.value = sitecontents ;     var n=sitecontents.search("this item not on sale");  document.write("value of n"+n);  if(n=-1) {  //alert("this item on sale. n:"+n);   var sitecontents2 = "this item on sale:"+itemname;  document.getelementbyid("mydiv").innerhtml += sitecontents2; };  });    };   items=["pen","paper","book","tshirt","cup","coffee","plate","cherry","apple","mango","orange"]; (var i=0;i<items.length;i++) { document.write(items[i] + "<br>");  getjsonresult(items[i]); } ////settimeout(getjsonresult('pen'), 500); ////settimeout(getjsonresult('paper'), 500); ////settimeout(getjsonresult('book'), 500); ////settimeout(getjsonresult('tshirt'), 500);  ////getjsonresult('cup'); ////getjsonresult('coffee'); ////getjsonresult('plate'); ////getjsonresult('cherry'); ////getjsonresult('apple'); ////getjsonresult('mango'); ////getjsonresult('orange');  </script>    </head>  <body> <br>    <div id="mydiv"></div>   <br>     </body> </html> 

did notice in if statement doing assignment?

if(n=-1)

should

if(n == -1)

or if want safe

if(n === -1)


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -