简体   繁体   中英

Win8 Metro App setInterval Javascript

I am new to building Metro Apps. I just want to create every second a new Rectangle and start with position x = 0

But with my code, it seems that the x+=10 is called before the first rectangle is drawn. But i dont know how i could change this.. could someone please help me out?

(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    WinJS.strictProcessing();

    var x = 0;
    var y = 0;

    function Clock(context) {

        context.fillRect(x, y, 9, 9);
        context.fillStyle = "red";

        x += 10;
    }

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                var canvas = document.getElementById("uhr");
                var context = canvas.getContext("2d");

                setInterval(function () { Clock(context) }, 1000);


            } else {

            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    app.oncheckpoint = function (args) {

    };


    app.start();
})();

Try storing the value of x and y inside of Clock() in a temporary and locally scoped variable.

function Clock(context) {
    var tmpX = x;
    var tmpY = y;
    context.fillRect(tmpX, tmpY, 9, 9);
    context.fillStyle = "red";

    x += 10;
}

I already solved it with doing context.fill(); before doing the x+=10;.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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