Created
May 28, 2023 20:01
-
-
Save fovelas/40890a6eb6fa257bc94e6d01d22b35cb to your computer and use it in GitHub Desktop.
Node.js Listener Kullanımı
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// game.model.js dosyamızı çağırıyoruz | |
const Game = require('./game.model.js'); | |
// yeni bir nesne oluşturuyoruz | |
var game = new Game('18815616', null, 30); | |
// bu nesneye özel bir listener oluşturuyoruz | |
game.setOnTickListener(() => { | |
console.log('game time: ' + game.time); | |
}); | |
game.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game { | |
// bu classtan bir nesne oluşturulduğu zaman listener varsayılan olarak null tanımlanıyor | |
onTick = null; | |
constructor(id, players, time) { | |
this.id = id; | |
this.players = players; | |
this.time = time; | |
} | |
// oyunu başlatıyoruz | |
start() { | |
var interval = setInterval(() => { | |
if (this.time > 0) { | |
this.time -= 1; | |
} | |
// burada listenerı tetikliyoruz | |
// her bir saniyede bu listener tetiklenecek | |
if (this.onTick != null) this.onTick(); | |
}, 1000); | |
} | |
// dışarıdan bu fonksiyonu kullanarak listener oluşturuyoruz | |
setOnTickListener(l) { | |
// bu kontrolü listener birden fazla kere tanımlanmasın diye koyuyoruz | |
if (this.onTick != null) return; | |
this.onTick = l; | |
} | |
} | |
module.exports = Game; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment