我有一个ajax函数,该函数返回一个json文件并将其直接推送到数组。我想使用上述数组在该数组中循环命名。但我有一个TypeError错误:names [[((((+ index)+ 1)%names.length)]未定义。有人能帮我吗。提前致谢。
var names = [];
var index = -1, looper, lastwinner;
$(document).on("click","#startraffle", function(){
var majorprize = ($('#majorprize').is(":checked")) ? 1 : 0;
$.ajax({
type: "POST",
url: baseUrl+"/home/getparticipants/"+majorprize,
dataType: 'json',
cache: false,
success: function(data){
$.each(data.participants, function (id, value) {
names.push({name: value.name, id: value.id});
});
}
});
console.log(names);
(function __cycle(){
var name = names[++index % names.length].name;
$("#winnername").text(name);
looper = setTimeout(__cycle, 500);
})();
$(this).attr("disabled",true);
});