looping through an array with a button in titanium -
i student apologize asking such simple question. trying make "next , "previous" button loops through array 1 @ time.
var food = ["pasta", "salad", "apple", "pizza"]; counter=0; var displayalert = function(){ (var i=0, item; i<food.length; i++) { item = food[i]; quotetext.text=food[i]; } }; var quoteview = ti.ui.createview({ backgroundcolor: "#fff", height: 150, top: 50, left: 20, right: 20, borderradius: 5 }); var quotetext = ti.ui.createlabel({ text: "click below begin", font: {fontsize: 20, fontfamily: "arial"}, textalign: "center" }); quoteview.add(quotetext); mainwindow.add(quoteview); buttonprevious.addeventlistener("click", displayalert); buttonnext.addeventlistener("click", displayalert);
so have worked through code instructor , although take 0 on assignment, else might benefit misfortune.
var food = ["pasta", "salad", "apple", "pizza"]; counter=0; var displayalert = function(){ console.log (counter); quotetext.text=food[counter]; if (counter === food.length-1){ counter=0; } else {counter=counter+1}; }; var getprevious = function(){ quotetext.text=food[counter]; if (counter === 0){ counter=food.length-1; } else {counter=counter-1}; }; var quoteview = ti.ui.createview({ backgroundcolor: "#fff", height: 150, top: 50, left: 20, right: 20, borderradius: 5 }); var quotetext = ti.ui.createlabel({ text: "click below begin", font: {fontsize: 20, fontfamily: "arial"}, textalign: "center" }); quoteview.add(quotetext); mainwindow.add(quoteview);
Comments
Post a Comment