jquery同步函数

发布于 2024-08-11 11:24:11 字数 147 浏览 1 评论 0原文

有没有办法在另一个函数完成后运行一个函数?例如:

doSomething();
doSomethingElse();

我只想在 doSomething 完成后运行 doSomethingElse() 。这可能吗?

Is there a way to run a function after another functions completes? For example:

doSomething();
doSomethingElse();

i only want doSomethingElse() to run after doSomething completes. is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慵挽 2024-08-18 11:24:11

如果您的 doSomething() 函数正在异步执行某些操作(例如发出 Ajax 请求),那么您需要让 doSomethingElse() 作为该异步请求的回调,而不是而不是在 doSomething() 返回后立即执行。

If your doSomething() function is doing something asynchronously (such as making an Ajax request) then you'll want to make doSomethingElse() the callback for that asynchronous request, rather than having it execute immediately after doSomething() returns.

你是我的挚爱i 2024-08-18 11:24:11

实际上你可以用 jQuery 做你想做的事(至少在某种意义上)。它可能被认为有点hacky但工作完美。

只需在页面中包含一个不用于其他用途的元素即可。例如某处的空 div 。

<div id="syncFnHolder"></div>

然后将 JS 包含在您的函数中 + 我编写的 runMe 函数。

function doSomething() { alert("First"); }
function doSomethingElse() { alert("Second"); }
function doSomethingElseThree() { alert("Third"); }

function runMe(fn, selec) {
    return function() {
        fn();
        setTimeout(function() {
            $(selec).dequeue("syncFnQueue");
        }, 1000);
    } 
};

var selector = "#syncFnHolder";
//add three functions to a queue
$(selector).queue("syncFnQueue", runMe(doSomething, selector));
$(selector).queue("syncFnQueue", runMe(doSomethingElse, selector));
$(selector).queue("syncFnQueue", runMe(doSomethingElseThree, selector));
//start first function in queue (FIFO) others are triggered automatically
$(selector).dequeue("syncFnQueue");

这将生成三个警报,它们之间的距离为 1 秒,分别为“First”、“Second”、“Third”。

如果您不想延迟,请删除 setTimeout 并将调用留给 $(selec).dequeue("syncFnQueue");

如果您的函数需要参数,例如 doSomething(x,y,z),您需要将 runMe 函数调整为更好的部分,并以不同的方式构造返回的函数。检查此处或发布新问题。

javascript 部分

Actually you can do what you want with jQuery (at least in some sense). It could be considered a little bit hacky but works flawless.

Just include an element in your page which you don't use for anything else. e.g. an empty div somewhere.

<div id="syncFnHolder"></div>

And then include the JS with your functions + the runMe function written by me.

function doSomething() { alert("First"); }
function doSomethingElse() { alert("Second"); }
function doSomethingElseThree() { alert("Third"); }

function runMe(fn, selec) {
    return function() {
        fn();
        setTimeout(function() {
            $(selec).dequeue("syncFnQueue");
        }, 1000);
    } 
};

var selector = "#syncFnHolder";
//add three functions to a queue
$(selector).queue("syncFnQueue", runMe(doSomething, selector));
$(selector).queue("syncFnQueue", runMe(doSomethingElse, selector));
$(selector).queue("syncFnQueue", runMe(doSomethingElseThree, selector));
//start first function in queue (FIFO) others are triggered automatically
$(selector).dequeue("syncFnQueue");

This would generate three alerts with a distance of 1s between them saying "First", "Second", "Third".

If you want no delay remove the setTimeout and just leave the call to $(selec).dequeue("syncFnQueue");.

If your functions need parameters e.g. doSomething(x,y,z) you need to adapt the runMe function to be a better partial and to construct the returned function differently. Check here or post new question.

javascript partial

如果没结果 2024-08-18 11:24:11

你可能想这样做......

function doSomething(callback) {
    // code for do some thing
    // now fire off the callback
    if (typeof callback === 'function') {
        callback();
    }
}

function doSomethingElse() {
}

doSomething(doSomethingElse);

You may want to do this....

function doSomething(callback) {
    // code for do some thing
    // now fire off the callback
    if (typeof callback === 'function') {
        callback();
    }
}

function doSomethingElse() {
}

doSomething(doSomethingElse);
任性一次 2024-08-18 11:24:11

问题有点模糊。

如果您的函数具有异步位,请使用回调。这就是他们的目的。

但是,如果您想在 JavaScript 中进行面向方面的编程,请查看 jQuery- aop

The question is a bit vague.

If your functions have asynchronous bits to them, then use callbacks. That's what they are for.

If, however, you want to do aspect-oriented programming in JavaScript, take a look at jQuery-aop.

伪心 2024-08-18 11:24:11

本身没有办法明确强制执行此操作,但一个想法是让 doSomething() 返回一个 doSomethingElse() 接受作为参数的值:

  retval = doSomething();
  doSomethingElse(retval);

这样至少可以防止有人在不提供参数的情况下调用 doSomethingElse()需要......当然,他们是否通过调用 doSomething() 获得该参数是另一个问题。但这是一个开始。

想法源自 Code Complete。

There's no way to enforce this explicitly per se, but one idea is to have doSomething() return a value that doSomethingElse() accepts as a parameter:

  retval = doSomething();
  doSomethingElse(retval);

that way at least you prevent somebody from calling doSomethingElse() without at least supplying the argument it needs... of course, whether they get that argument from calling doSomething() is another issue. But this is a start.

Idea lifted from Code Complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文