最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Best way to put delay after calling javascript functions - Stack Overflow

programmeradmin0浏览0评论

I'm using jquery to call some javascript functions with a delay between them.
Also I'm using Jquery Wait

When I call below function,all functions are called recpectively,there are no delays between each other.

$(this)
.call(f1)
.wait(5000)
.call(f2)
.wait(5000)
.call(f3);

Here call function calls some function as I did

$.fn.call = function (f) {
    if (f)
        f();

    return this;
};

What am i doing wrong ? How can i achieve something like this ?
Thank you

I'm using jquery to call some javascript functions with a delay between them.
Also I'm using Jquery Wait

When I call below function,all functions are called recpectively,there are no delays between each other.

$(this)
.call(f1)
.wait(5000)
.call(f2)
.wait(5000)
.call(f3);

Here call function calls some function as I did

$.fn.call = function (f) {
    if (f)
        f();

    return this;
};

What am i doing wrong ? How can i achieve something like this ?
Thank you

Share Improve this question edited Oct 7, 2010 at 8:15 Myra asked Oct 7, 2010 at 8:07 MyraMyra 3,6563 gold badges39 silver badges47 bronze badges 2
  • What, exactly, isn't working here? – Samir Talwar Commented Oct 7, 2010 at 8:10
  • functions are called one by one,there are no delays between them – Myra Commented Oct 7, 2010 at 8:14
Add a ment  | 

4 Answers 4

Reset to default 6

If you want to call a function every 5 seconds use

setTimeout(function(){f1},5000);
setTimeout(function(){f2},10000);
setTimeout(function(){f2},15000);

if you want to call each function 5 seconds after the last one terminated use

setTimeout(function(){f1;setTimeout(function(){f2;setTimeout(function(){f3},5000);},5000);},5000);

You don't need wait() from that cookbook; delay() is built-in and appears to have the same functionality. But either function involves adding something to jQuery's internal queue of effects and then removing it after a timeout expires, i.e. it's not a sleep statement, so it's not going to wait around before returning.

If you want to use delay() or wait(), you should make call() enqueue the function with queue(). Just sketching, but something like:

$.fn.call = function(f) {
    if (f) {
        $(this).queue(function() {
            f();
            $(this).dequeue();
        }
    }
    return this;
}

Then I'd expect your code to work the way you intend.

Here is a function that calls in sequence an array of function:

$.fn.callFn = function(fns, delay) {
    var fn, that = this;
    if(fns.length > 0){
        fn = fns.shift()
        fn && fn();
        setTimeout(function(){
            that.callFn(fns, delay);
        }, delay);
    }
    return this;
};

And you would call it like that:

$(this).callFn([f1, f2, f3], 2000);
$('#box').slideUp(300).delay(800).fadeIn(400);

/* .delay = wait time = 800 (this means it will wait 800/1000 of a second/ "1000 = 1 second") */
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>