仅在另一个给定函数完全执行后执行函数

时间:2018-04-16 21:35:39

标签: javascript jquery

我有这段代码,由于同步问题,我需要确保在运行setConsoleWidth()函数之前已经执行了所有这些MathJax。我怎么能这样做? 我更喜欢本机解决方案,但我真的不介意使用jQuery。

window.updateConsole = function() {
  MathJax.Hub.Queue(["Rerender",MathJax.Hub,"math"]);
  MathJax.Hub.Queue(function() {
    var math = MathJax.Hub.getAllJax("mathDiv")[0];

    MathJax.Hub.Queue(["Text", math, "R( \\theta ) = sin^{ \\class{hover P}{" + P.show + "} } \\left ( \\frac{\\class{hover B}{" + sign_mult(B.show) + "} ⋅ \\class{hover S}{" + sign_mult(S.show) + "} ⋅ \\class{hover J}{" + sign_mult(J.show) + "} ⋅ \\theta ⋅ ( \\theta \\class{hover S}{" + sign_sum(-(S.show)) + "})}{\\class{hover N}{" + N.show + "}} \\right ) \\;  \\mapsto  \\;  \\left\\{\\begin{array}i x(\\theta) = \\class{hover C}{" + C.show + "} \\class{hover E}{"  + sign_sum(E.show) + "} ⋅ R(\\theta) ⋅ cos^{\\class{hover H}{" + H.show + "}}(\\theta)\\\\y(\\theta) =\\class{hover D}{" + D.show + "} \\class{hover F}{" + sign_sum(F.show) + "} ⋅ R(\\theta) ⋅ sin^{\\class{hover Z}{" + Z.show + "}}(\\theta)\\end{array}\\right."]);
    MathJax.Hub.Queue(setConsoleWidth);
    MathJax.Hub.Queue(console.log(Console.scrollWidth));
  });
};

function setConsoleWidth() {
  MathJax.Hub.Queue(function () {
    var w = Console.scrollWidth, W = window.innerWidth*0.85;

    if (w > W) {
      console.log("bingo");
      math.style.fontSize = (2.8*W/w)+"vw";
    }
 });
}

2 个答案:

答案 0 :(得分:1)

只需将控制台功能添加到最后的队列中,以确保只有在完成所有先前操作后才执行:

Console.Read()

答案 1 :(得分:-1)

我想也许这个(假设MathJax.Hub.Queue(function (){}内的调用是同步的):

window.updateConsole = function () {
    function done() {
        setConsoleWidth();
    }
    MathJax.Hub.Queue(["Rerender", MathJax.Hub, "math"]);
    MathJax.Hub.Queue(function () {
        var math = MathJax.Hub.getAllJax("mathDiv")[0];
        MathJax.Hub.Queue(["Text", math, "R( \\theta ) = sin^{ \\class{hover P}{" + P.show + "} } \\left ( \\frac{\\class{hover B}{" + sign_mult(B.show) + "} ⋅ \\class{hover S}{" + sign_mult(S.show) + "} ⋅ \\class{hover J}{" + sign_mult(J.show) + "} ⋅ \\theta ⋅ ( \\theta \\class{hover S}{" + sign_sum(-(S.show)) + "})}{\\class{hover N}{" + N.show + "}} \\right ) \\;  \\mapsto  \\;  \\left\\{\\begin{array}i x(\\theta) = \\class{hover C}{" + C.show + "} \\class{hover E}{" + sign_sum(E.show) + "} ⋅ R(\\theta) ⋅ cos^{\\class{hover H}{" + H.show + "}}(\\theta)\\\\y(\\theta) =\\class{hover D}{" + D.show + "} \\class{hover F}{" + sign_sum(F.show) + "} ⋅ R(\\theta) ⋅ sin^{\\class{hover Z}{" + Z.show + "}}(\\theta)\\end{array}\\right."]);

        done();
    });
};