简体   繁体   English

我的功能没有回来

[英]My function isn't returning

Ehllo! Ehllo! My function isn't returning (exiting) when conditions are met. 当满足条件时,我的功能不会返回(退出)。

var arrowReady = false;
var arrowImage = new Image();
var deg = 0;
arrowImage.onload = function () {
    arrowReady = true;
    function moveArrow() {
        setInterval(function() {
            ctx1.save();
            deg++;
            ctx1.rotate(deg * Math.PI / 180);
            // Here is  ^  the amount of degrees it turns.
            ctx1.clearRect(300, 200, 52, 310);
            ctx1.drawImage(arrowImage, 300, 100, 42, 300);
            ctx1.restore();
        }, 100);

        if (deg == 40) return;

    }
}

From my understanding, when deg = 40, the function should stop. 根据我的理解,当deg = 40时,函数应该停止。 But, it's not. 但是,事实并非如此。 Any suggestions? 有什么建议么?

Tricky javascript going on here. 棘手的javascript在这里发生。 moveArrow is only called once on document load. moveArrow仅在文档加载时调用一次。 returning or not will not stop your interval from running. 是否返回不会阻止你的间隔运行。

You need to save a reference to the interval: 您需要保存对间隔的引用:

var interval = setInterval(...

And then from WITHIN the anonymous setInterval function, when deg ==40 you will want to clearInterval(interval) to stop it from running. 然后从WITHIN匿名setInterval函数,当deg ==40你将想要clearInterval(interval)来阻止它运行。

Try this: 尝试这个:

var arrowReady = false;
var arrowImage = new Image();
var deg = 0;
arrowImage.onload = function () {
    arrowReady = true;
    function moveArrow() {
        var interval = setInterval(function() {
            if (deg == 40) {
              clearInterval(interval);
              return;
            }
            ctx1.save();
            deg++;
            ctx1.rotate(deg * Math.PI / 180);
            // Here is  ^  the amount of degrees it turns.
            ctx1.clearRect(300, 200, 52, 310);
            ctx1.drawImage(arrowImage, 300, 100, 42, 300);
            ctx1.restore();
        }, 100);

    }
}

I am not a JavaScript programmer but don't you need to specify a value to return 我不是JavaScript程序员,但您不需要指定要返回的值

EXAMPLE if (deg == 40) return deg; 示例if (deg == 40) return deg;

It's not clear what that last conditional is even supposed to do. 目前尚不清楚最后一个条件甚至应该做什么。 There's nothing after it, so the function is going to stop execution there anyway. 之后什么也没有,所以无论如何,该功能将停止执行。 Why does it matter if deg == 40 if that's where the function call ends no matter what? 如果这是函数调用结束的地方,那么为什么如果deg == 40则无关紧要?

More to the point, I'm sure, is the fact that the function in question doesn't actually change the value of deg . 更重要的是,我确信,这个函数实际上并没有改变 deg的值。 So deg will never equal 40 on that line. 所以deg在那条线上永远不会等于40 What you're doing is queuing up another function to run at a later time (100 ms?). 你正在做的是排队另一个函数以便稍后运行(100毫秒?)。 After that function is queued up, the control immediately goes to the if statement (before that other function runs). 在该函数排队后,控件立即转到if语句(在其他函数运行之前)。

So deg is being modified, in a completely different function scope, after the if statement executes. 因此,在if语句执行之后,在一个完全不同的函数范围内修改deg

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

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