Skip to content

Instantly share code, notes, and snippets.

@fovelas
Created May 28, 2023 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fovelas/40890a6eb6fa257bc94e6d01d22b35cb to your computer and use it in GitHub Desktop.
Save fovelas/40890a6eb6fa257bc94e6d01d22b35cb to your computer and use it in GitHub Desktop.
Node.js Listener Kullanımı
// 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();
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