如何在JavaScript中放入睡眠功能?

时间:2017-02-07 06:52:34

标签: javascript html5

我正在使用javascript在html5中创建一个游戏,我遇到了睡眠功能问题。我想暂停一个isWin函数几秒钟,然后显示一个弹出窗口,声明该人赢了游戏。这是代码的一部分。

modal: function (isWin) {
	var bgDarker = this.game.make.bitmapData(this.game.width, this.game.height);
	bgDarker.fill(50, 50, 50);
	bgDarker = this.game.add.button(0, 0, bgDarker, function () { }, this);
	bgDarker.tint = 0x000000;
	bgDarker.alpha = 0.5;

	var modalGroup = this.game.add.group();

	var bg = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY - 40,  (isWin ? "picWinPopup" : "picLostPopup"));
	bg.scale.setTo(1);
	bg.anchor.setTo(0.5);
	modalGroup.add(bg);
	
	var labelLevel2 = this.game.add.text(this.game.world.centerX + 100, this.game.world.centerY + 5, MatchingGame.indexImage, {
		font: "48px " + MatchingGame.fontFaces[3],
		fill: "#1a40ff",
		boundsAlignH: "center",
		boundsAlignV: "middle"
	});
	labelLevel2.anchor.setTo(0.5);
	modalGroup.add(labelLevel2);
    
	var labelTotalScore = this.game.add.text(this.game.world.centerX, this.game.world.centerY + 110, MatchingGame.score, {
		font: "34px " + MatchingGame.fontFaces[3],
		fill: "#ff2a5c",
		boundsAlignH: "center",
		boundsAlignV: "middle"
	});
	labelTotalScore.anchor.setTo(0.5);
	modalGroup.add(labelTotalScore);
	
	var btnReplay = this.makeButton("btnReplay", function (close) {
		btnReplay.bgDarker.destroy();
		btnReplay.modalGroup.destroy();
		if(MatchingGame.indexImage==6){
			MatchingGame.indexImage = 1;
		}
		this.game.stateTransition.to('Game');
    	this.imgLive1.visible = true;
    	this.imgLive2.visible = true;
    	this.imgLive3.visible = true;
    	MatchingGame.live = 3;
    	MatchingGame.levelscore = 0;
	},this);
    //btnReplay.anchor.setTo(0.5);
	btnReplay.position.setTo(bg.position.x - 200, bg.position.y + 260)
	btnReplay.bgDarker = bgDarker;
	btnReplay.modalGroup = modalGroup;
	modalGroup.add(btnReplay);
	
	if (!isWin) {
		var btnEnd = this.makeButton("btnEnd", function (close) {
			btnEnd.bgDarker.destroy();
			btnEnd.modalGroup.destroy();
			
        	this.imgLive1.visible = true;
        	this.imgLive2.visible = true;
        	this.imgLive3.visible = true;
        	MatchingGame.live = 3;
        	
        	this.end();
		},this);
		btnEnd.position.setTo(bg.position.x + 200, bg.position.y + 260)
		btnEnd.bgDarker = bgDarker;
		btnEnd.modalGroup = modalGroup;
		modalGroup.add(btnEnd);
    }
    else {
    	//setTimeout(3000);
    	//var TimeHandler = setTimeout(function(){clearTimeout(TimeHandler);},3000);

    	var btnNextLevel = this.makeButton("btnNextLevel", function (close) {
			btnNextLevel.bgDarker.destroy();
			btnNextLevel.modalGroup.destroy();
			if(MatchingGame.indexImage==6){
				MatchingGame.indexImage = 1;
			} else {
				MatchingGame.indexImage = MatchingGame.indexImage+1;																	}
			this.game.stateTransition.to('Game');
        	this.imgLive1.visible = true;
        	this.imgLive2.visible = true;
        	this.imgLive3.visible = true;
        	MatchingGame.live = 3;
		},this);
		btnNextLevel.position.setTo(bg.position.x + 200, bg.position.y + 260)
		btnNextLevel.bgDarker = bgDarker;
		btnNextLevel.modalGroup = modalGroup;
		modalGroup.add(btnNextLevel); 
    }
    
    MatchingGame.live = 3;
	
	this.game.add.tween(modalGroup).from({ y: -800 }, 600, Phaser.Easing.Bounce.Out, true);
	this.game.world.bringToTop(modalGroup);
}, 

我确实尝试使用sleep()和setTimeout()函数但是仍然无法在javascript中实现它。你能帮我解决把这个定时器放在javascript函数中的问题吗?因为我在使用html5的javascript代码中集成setTimeout时遇到问题。

3 个答案:

答案 0 :(得分:0)

像这样使用

setTimeout(function() {
var btnNextLevel = this.makeButton("btnNextLevel", function (close) {
                btnNextLevel.bgDarker.destroy();
                btnNextLevel.modalGroup.destroy();
                if(MatchingGame.indexImage==6){
                    MatchingGame.indexImage = 1;
                } else {
                    MatchingGame.indexImage = MatchingGame.indexImage+1;                                                                    }
                this.game.stateTransition.to('Game');
                this.imgLive1.visible = true;
                this.imgLive2.visible = true;
                this.imgLive3.visible = true;
                MatchingGame.live = 3;
            },this);
            btnNextLevel.position.setTo(bg.position.x + 200, bg.position.y + 260)
            btnNextLevel.bgDarker = bgDarker;
            btnNextLevel.modalGroup = modalGroup;
            modalGroup.add(btnNextLevel); }, 3000 );

答案 1 :(得分:0)

javascript是单线程,因此没有睡眠/等待/停止。 你永远不应该只让单个线程停止工作。 如果你想延迟一些代码,请使用setTimeout。 但是使用setTimeout的正确方法是:

setTimeout(myFunction, 40000);

这是一个例子。 详情请见:

mdn setTimeout

答案 2 :(得分:0)

这可能符合您的要求     var TimeHandler = setTimeout(function(){clearTimeout(TimeHandler);},3000);