需要帮助以幻灯片方式显示数组项

时间:2018-04-04 20:09:15

标签: javascript arrays

我的html / css和javascript的副本。 Stackoverflow希望我添加更多信息,但我认为标题非常自我解释(听起来不像d ** k)。我想以幻灯片方式显示每个理发师的名字和他们的社交媒体信息。我希望理发师的名字和信息每4秒钟就会改变一次。我不想要答案我想知道我做错了什么,所以我不会在将来重复这个错误。谢谢。 :)

                                  

                                       

             
function showinfo(){
    var barbername = ['Brian', 'Dwight', 'Greg','Josh','Tino'];
    var barberinfo = ['BarberPro','','Sunnti_TSN', '', 'ClipperDreams'];

    var bnameholder = document.getElementById('barbernamesm');
    var binfoholder = document.getElementById('barberinfosm');

    for (i = 0; i < barbername.length; i++){
        var currentname = barbername[i];
        var currentinfo = barberinfo[i];
    }

        bnameholder.innerText(currentname);
        binfoholder.innerText(currentinfo);

        setTimeout(showinfo, 4000);
    }

    showinfo();

1 个答案:

答案 0 :(得分:2)

有几个问题,但主要问题是 - 您需要为每次i电话增加showInfo()次。

此外,不要使用两个数组,而应考虑一个数组,其中的对象包含每个理发师的名称和信息 - [{ name: 'Brian', info: 'BarberPro' }, ...]

&#13;
&#13;
var i = 0; // should be available to all runs of showinfo()
var barbername = ['Brian', 'Dwight', 'Greg', 'Josh', 'Tino']; // declare once
var barberinfo = ['BarberPro', '', 'Sunnti_TSN', '', 'ClipperDreams']; // declare once
var bnameholder = document.getElementById('barbernamesm'); // get and cache once
var binfoholder = document.getElementById('barberinfosm'); // get and cache once

function showinfo() {
  var currentname = barbername[i];
  var currentinfo = barberinfo[i]; 

  bnameholder.innerText = currentname; // innerText is a property and not a function
  binfoholder.innerText = currentinfo; // innerText is a property and not a function
  
  i = (i + 1)  % barbername.length; // increment i in cycle of 0-3

  setTimeout(showinfo, 1000);
}

showinfo();
&#13;
<div id="barbernamesm"></div>
<div id="barberinfosm"></div>
&#13;
&#13;
&#13;