So I have this function
function main()
{
//get the start text
var start_text = document.getElementById("start_text").value;
//get target text
var target_text = document.getElementById("target_text").value;
// get characters to use for modifying string
var characters = document.getElementById("characters").value;
// get the mutation rate
var mutation_rate = document.getElementById("mutation_rate").value;
// get the amount of offspring for each generation
var amount_offspring = document.getElementById("amount_offspring").value;
// send all the input into generation, to generate a generation of offspring.
// this function will return the highest scoring offspring of each generation
best_offspring = generation(start_text, characters, amount_offspring, mutation_rate, target_text, generations);
// keep looping untill the target_text is met
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 the amount of generations
document.getElementById("generations").value = generations;
}
}
Everything works, except that it outputs the finnished string when the while loop is done.
What I want is for it to output each generations best_offspring so that you can see the string "evolve" in real time. I tried to use the function setTimout() like such:
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;
}
but didnt get it to work. Any ideas?
#output_textinstead of overwriting its value?setTimeoutreturns its id, and calls the function you pass as the first parameter after the number of milliseconds you pass as the second parameter. Right now you are calling your functiongeneration()before you pass it in (notice the()). This should help, its an example using ajax but this is still a way to deal with any asynchronous functions likesetTimeoutajaxto sendasynchronouscalls to the function. But as far as I understod is that with the help of ajax I can sendasynchronous calls to theserver. But since all this is client side, will it still work?setTimeoutbehaves similarly in the sense that it takes in a function (callback) that is called on some event (after certain amount of milliseconds)