I need help with a function I have been working on.
function main()
{
//retrieve the start text
var start_text = document.getElementById("start_text").value;
//retrieve target text
var target_text = document.getElementById("target_text").value;
//obtain characters for string modification
var characters = document.getElementById("characters").value;
//get mutation rate
var mutation_rate = document.getElementById("mutation_rate").value;
//get number of offspring for each generation
var amount_offspring = document.getElementById("amount_offspring").value;
//pass all input to generation function to create offspring generations.
//this function will return the highest scoring offspring from each generation
best_offspring = generation(start_text, characters, amount_offspring, mutation_rate, target_text, generations);
//continue looping until the target_text is reached
while(score_met == false)
{
//send the highest scoring offspring again
best_offspring = generation(best_offspring, characters, amount_offspring, mutation_rate, target_text);
//output the best offspring
document.getElementById("output_text").value = best_offspring;
//output number of generations
document.getElementById("generations").value = generations;
}
}
Everything is functioning correctly, except that it only outputs the final string once the while loop finishes.
I am looking for a way to display each generation's best_offspring
in real time as the process evolves. I attempted using setTimeout()
like this:
while(score_met == false)
{
//send the highest scoring offspring again
best_offspring = setTimeout(generation(), 1000, best_offspring, characters, amount_offspring, mutation_rate, target_text);
document.getElementById("output_text").value = best_offspring;
document.getElementById("generations").value = generations;
}
However, I was unable to get it to work. Any suggestions?