简体   繁体   中英

Looping through an array with flexslider

In my javascript below I would like to loop through each array (which looks like this):

["apple", "microsoft", "reddit"] 

The array could have 3 + values...

BUT I would like to loop through each array one at a time (kinda delayed I guess?) and to be placed into the name variable separately. Let me try to explain better....

I'm using a image slider and it has a "after function" that triggers after a slide has completed. I'm trying to pass one array value at a time when the "after function" triggers and send the array value (for example "apple") to data of ajax post.

Ugh.. I've done research for hours and had no luck, so now I'm asking for some help please! I hope this make some sense! Any help/feedback is really appreciated!

Javascript

var javascript_array = <?php echo $js_array; ?>;
console.log(javascript_array.map(a => a.company));
// Output ["apple", "microsoft", "reddit"] 

var name = '';

$(".flexslider").flexslider({
  animation: "fade",
  controlsContainer: ".flexslider",
  slideshow: true,
  keyboard: false,
  randomize: false,
  slideshowSpeed: "20000",
  controlNav: false,
  directionNav: false,

  after: function(slider){
    $.ajax({
        type: "POST",
        cache: false,
        url: "file.php",
        data: { companyName: name},
        success: function(response) {
        }
    });
  }

});
</script>

You should remember the index of current item.


 let array = ["apple", "microsoft", "reddit"] let index = 0; function callback(){ if(index>=array.length){ console.log('finish') return } let name = array[index] ++index; console.log({companyName: name}) } callback() callback() callback() callback()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM