I've seen many others ask questions to do with this but none of the answers actually work and don't apply to my situation.
I just simply want to implement a delay within my for each loop of variable time lengths. These variable time lengths come from the array I am looping through when reading the json file. To give further context, the aim is to highlight each word in a sentence for a variable amount of time - each word has timing in milliseconds associated with it from the json file.
My assumption is that the solution is something to do with setTimeout but every time I try that it waits initially but then skips all the rest.
My code right now:
$("[id^='timing_check']").click(function() {
sn = $(this).attr('name');
ayahnum = $(this).attr('data-id');
$.getJSON("/static/json/mishary.json", function(result) {
$.each(result, function(i, v) {
if (v.ayah == ayahnum && v.surah == sn) {
var x = v.segments;
var i = 1;
x.forEach(function(item) {
time_sleep = item[3];
wordref = sn + ':' + ayahnum + ':' + i;
i++;
setTimeout(function() {
$("[name='" + wordref + "']").css('color', 'red');
}, time_sleep);
});
}
});
});
});
});
This doesn't work at all. Not even close. Please offer some guidance