Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
66 views
in Technique[技术] by (71.8m points)

javascript - Timer is not running as expected

In my tank game (not unlike awesome tanks for reference) I have created a timer for my bullet but when run it has a runtime error saying that AItimer is not defined. Im confused since i have another timer which in the same program but does not have this error.

function aiStartTimer()
{
    if(shoot == 0)
    {
        //creates aitimer variable
        AItimer = setInterval("aiFireBullet()",100);
        shoot = 1*1;
    }
    else if(shoot == 1)
    {
        clearInterval(AItimer);
        shoot = 0 * 1;
    }
}

function StartTimer()
{
    if(onOff == 0)
    {
        //creates timer variable
        timer = setInterval("FireBullet()",100);
        onOff = 1*1;
    }
    else if(onOff == 1)
    {
        clearInterval(timer);
        onOff = 0 * 1;
    }

}

here is a fiddile https://jsfiddle.net/tm9oL74r/

question from:https://stackoverflow.com/questions/65926715/timer-is-not-running-as-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to pass the function definition in argument of setTimeout, not a string. Replace AItimer = setInterval("aiFireBullet()", 100); by AItimer = setInterval(aiFireBullet, 100); and it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...