简体   繁体   English

Mootools,如何使每个循环异步

[英]Mootools, how to make an asynchronous each loop

JS Fiddle code JS小提琴代码

var id = 1;
var lala = new Array(
    'Hello World',
    'This is my new and fancy console!',
    'See how its typing?',
    'Isn\'t that cool ? ',
    'Try it out for yourself!');
lala.each(function (line) {
    id++;
    fullID = 'Div' + id;
    new Element('p', {
        'id': fullID
    }).inject($(document.body));
    texttype(fullID, line, 100, 100);
});

I'm trying to call a method in an MooTools each loop, but the problem is that due to javascripts nature all method calls are called at aprox. 我正在尝试在MooTools每个循环中调用一个方法,但是问题是由于javascripts的性质,所有方法调用都在aprox处调用。 the same time. 同一时间。 How do I prevent this from happening? 如何防止这种情况发生? Do I need to return something from the method I call? 我需要从调用的方法中返回一些东西吗? Is there another way? 还有另一种方法吗? Arbitrary timeouts don't really seem to be a good solution because of timing issues. 由于时序问题,任意超时似乎都不是一个好的解决方案。 (Note, this code is in testing stage, so its bogus data and var names) (注意,此代码处于测试阶段,因此其伪造的数据和var名称)

In the texttype function there's a fifth argument, which is the callback function that's called after the text is typed: texttype函数中,有第五个参数,这是在键入文本后调用的回调函数:

var doType = function(i) {
    var fullID = 'Div' + (i + 1); // Don't forget var!
    new Element('p', {
        'id': fullID
    }).inject($(document.body));
    texttype(fullID, lala[i], 100, 100, function() {
        doType(i + 1);
    });
}
doType(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM