0

我的问题是:我有两个 jquery 函数,它们都填写表单中的字段。目前它们同时运行,但我怎样才能让它在第一个脚本完成后才运行第二个脚本?

var string  = 'joe@quadwtech.com',
    letters = string.split(''),
    total   = letters.length,
    index   = 0,
    timer   = setInterval(function () {
        if (index < total) {
            $('input[name="email"]').val(function () {
                return $(this).val() + letters[(index++)];
            });
        } else {
            clearInterval(timer);
        }
    }, 100);
var stringtwo  = 'Jason',
    ttert = stringtwo.split(''),
    totaltwo   = ttert.length,
    countt   = 0,
    timer   = setInterval(function () {
        if (countt < totaltwo) {
            $('input[name="first"]').val(function () {
                return $(this).val() + ttert[(countt++)];
            });
        } else {
            clearInterval(timer);
        }
    }, 100);
4

2 回答 2

1

清除第一个后,您可以开始第二个。

var string  = 'joe@quadwtech.com',
letters = string.split(''),
total   = letters.length,
index   = 0;

var timer   = setInterval(function () {
    if (index < total) {
        $('input[name="email"]').val(function () {
            return $(this).val() + letters[(index++)];
        });
    } else {
        clearInterval(timer);
        var stringtwo  = 'Jason',
            ttert = stringtwo.split(''),
            totaltwo   = ttert.length,
            countt   = 0;

        timer = setInterval(function () {
                         if (countt < totaltwo) {
                             $('input[name="first"]').val(function () {
                                 return $(this).val() + ttert[(countt++)];
                             });
                         } else {
                             clearInterval(timer);
                         }
                      }, 100);
    }
}, 100);

或类似的东西。

于 2013-10-31T21:12:21.393 回答
0

Something like this: reference Wait till a Function with animations is finished until running another Function

API : http://api.jquery.com/deferred.done/

This might fit your needs :)

Code

var FunctionOne = function () {
  var string  = 'joe@quadwtech.com',
    letters = string.split(''),
    total   = letters.length,
    index   = 0,
    timer   = setInterval(function () {
        if (index < total) {
            $('input[name="email"]').val(function () {
                return $(this).val() + letters[(index++)];
            });
        } else {
            clearInterval(timer);
        }
    }, 100);
};

// define FunctionTwo as needed
var FunctionTwo = function () {

var stringtwo  = 'Jason',
    ttert = stringtwo.split(''),
    totaltwo   = ttert.length,
    countt   = 0,
    timer   = setInterval(function () {
        if (countt < totaltwo) {
            $('input[name="first"]').val(function () {
                return $(this).val() + ttert[(countt++)];
            });
        } else {
            clearInterval(timer);
        }
    }, 100);
};

// call FunctionOne and use the `done` method
// with `FunctionTwo` as it's parameter
FunctionOne().done(FunctionTwo);
于 2013-10-31T21:06:20.863 回答