LoginSignup
9
4

More than 3 years have passed since last update.

JavaScriptで配列を指定時間で回す

Last updated at Posted at 2018-06-27

JavaScriptを使って配列を、指定時間で回す

2019/06/18 コードを書き換えたものを下に載せています

用途
ゲーム作成時に使えそう(?)
擬似的な非同期処理として使えそう(?)

JSでゲームを作る際、ループ処理で配列を回す場合が多いです
でも、ループ処理で配列から値を取得する際は一瞬で終ってしまいますし、
実行環境の処理速度によって取得時間はバラバラになります。

配列自身から値を取得する時、その取得する時間を指定して
直接関数内で動作出来たらなと思って作ってみました。

動作する物は「一次元配列」に限ります

時間の指定は 1000/1秒 で可能です。

これを使えばRPGやシューティングゲーム開発に使えるかも...?

クソソースですが誰かの役立てばいいなと書いてます。

関数

javascript
function ArrayRoops(array, time, fun){
    this.length = array.length;
    this.counts = 0;
    this.Timer = setInterval(() => {
        this.key = this.counts;
        this.value = array[this.key];
        fun(this.key, this.value);
        this.counts ++;
        if(this.length <= this.counts){
            this.counts = 0;
            clearInterval(this.Timer);
        }
    }, time);
}

使用例

javascript

// 1次元配列
const array = ["dog", "cat", "horse"];

// 配列を 1秒間 に 1回 づつ回す
ArrayRoops(array, 1000, function(key, value){
    // 引数は key と value
    console.log(key + " : " + value);
});

// 配列を 3秒間 に 1回 づつ回す
ArrayRoops(array, 3000, function(key, value){
    // 引数は key と value
    console.log(key + " : " + value);
});

function ArrayRoops(array, time, fun){
    this.length = array.length;
    this.counts = 0;
    this.Timer = setInterval(() => {
        this.key = this.counts;
        this.value = array[this.key];
        fun(this.key, this.value);
        this.counts ++;
        if(this.length <= this.counts){
            this.counts = 0;
            clearInterval(this.Timer);
        }
    }, time);
}

出力結果

console
#配列 1
2018/06/27 00:00:00 | 0 : dog
2018/06/27 00:00:01 | 1 : cat
2018/06/27 00:00:02 | 2 : horse

#配列 2
2018/06/27 00:00:00 | 0 : dog
2018/06/27 00:00:03 | 1 : cat
2018/06/27 00:00:06 | 2 : horse

2019/06/18 変更

const roop = function(fun, time) {
    const self = new Object();
    const keys = Object.keys(this);
    self.count = 0;
    self.Timer = setInterval(() => {
        self.key = keys[self.count];
        self.value = this[self.key];
        if (fun.length >= 2) {
            fun(self.key, self.value);
        } else if (fun.length == 1) {
            fun(self.value);
        } else {
            fun.call({
                key: self.key,
                value: self.value
            });
        }
        self.count++;
        if (keys.length <= self.count) {
            self.count = 0;
            clearInterval(self.Timer);
        }
    }, time);
};
Object.prototype.roop = roop;
Array.prototype.roop = roop;

使用例

const array = ["array0", "array1"];
const object = {"key0":"object0", "key1":"object1"};

array.roop(function(key, value) {
  console.log(key, value);
}, 1000);
array.roop(function(value) {
  console.log(value);
}, 1000);
array.roop(function() {
  console.log(this.key, this.value);
}, 1000);


object.roop((key, value) => {
  console.log(key, value);
}, 1000);
object.roop(value => {
  console.log(value);
}, 1000);
object.roop(function() {
  console.log(this.key, this.value);
}, 1000);

一番うまく書けた気がする

9
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
4