lundi 1 décembre 2014

Wait for Javascript function finished inside foreach cshtml?


I'm trying to make simple autotype with Javascript. My problem is the autotype is cannot run sequentially if i call it from view (.cshtml).


My cshtml like this:



<script type="text/javascript" src="@Url.Content("/Scripts/autotype.js")"></script>

@foreach (var temp in @Model)
{
<script>
auto_type("ABCDEFG", 0)
</script>
}

<div id="divauto"></div>


and it's autotype.js :



function auto_type(wrt, i) {

wrt = wrt.split('');
var delay = 100;
while (i < wrt.length) {
setTimeout(function () {
$('#divauto').append(wrt.shift())
}, delay * i)
i++;
}
}


From these codes, the output will be like "AAABBBCCCDDDEEEFFFGGG" but i need the output like: "ABCDEFGABCDEFGABCDEFG"





Aucun commentaire:

Enregistrer un commentaire