简体   繁体   English

使用setInterval将动态变量传递给函数

[英]Passing dynamic variables to function using setInterval

I am trying to pass "dynamic" (data is changed frequently by keypress event) body and direction . 我试图传递“动态”( direction事件经常更改数据)的bodydirection

I am calling function to like this: 我正在调用函数来这样:

snakeGame.createInterval(snakeGame.move, grid, body, direction, interval);



  createInterval: function(f, grid, body, direction, interval) {
    setInterval(function() { f(grid, body, direction); }, interval);
  },

Problem is that both body and direction always keep same data when function was called. 问题在于,调用函数时, bodydirection始终保持相同的数据。

Let's say that direction was 'right' when calling it first time and before second occurence happens, direction was changed to 'up' , but this function still reads it as old 'right' . 假设第一次调用时direction'right' ,并且在第二次发生之前, direction已更改为'up' ,但此功能仍将其读取为旧的'right'

How do I make sure that passed parameters are always up-to-date? 如何确保传递的参数始终是最新的?

(Let me know if you need further clarification) (让我知道是否需要进一步说明)

To see changes to values, you need references to those values, and make sure that both the code that changes values and the code that read the values use a reference to the same value. 要查看值的更改,您需要引用这些值,并确保更改值的代码和读取值的代码都使用对相同值的引用。

There are several ways to do this, but it will involve an object with several object properties which will have the targetted values. 有多种方法可以执行此操作,但是它将涉及具有多个对象属性的对象,这些对象属性将具有目标值。 But you will pass the object, not the individual values. 但是您将传递对象,而不是单个值。

From the little you have provided, it looks like you have a snakeGame object. 从您提供的内容来看,您似乎有一个snakeGame对象。 You could use that object to store some of the properties, for instance, it could have a property grid . 您可以使用该对象存储一些属性,例如,它可以具有属性grid But you could also introduce another object, for storing things you'd rather not have in your snakeGame object. 但是您也可以引入另一个对象,用于存储您不想在snakeGame对象中拥有的东西 This could be the case for body and/or direction . 身体和/或方向可能就是这种情况。

For instance: 例如:

// Use object properties instead of separate variables: 
var snake = {
    body: 'xxxx',
    direction: 'L'
}

var snakeGame = {
    // You can also add properties to your existing object:
    grid: [[0,0,0],[0,0,0],[0,0,0]],
    createInterval: function(f, snake, interval) {
        // Pass `this` and snake objects:
        setInterval(f.bind(this, snake), interval); 
    },
    move: function(snake) {
        // use this.grid, snake.body and snake.direction: 
        //....
    }
    // etc
};

// Call with objects only (except for the constant interval)
snakeGame.createInterval(snakeGame.move, snake, interval);

// When you change direction:
$('#up').keydown(function () {
    // ... do not assign to snake, but to snake.direction:
    snake.direction = 'U';
    // ...
    // Same principle with snake.body.
});

I just chose to define grid as a property of snakeGame , and direction and body as properties of a newly introduced object. 我只是选择将grid定义为snakeGame的属性,而将directionbody定义为新引入对象的属性。 But you may want to do it differently. 但是您可能想要以不同的方式进行操作。 This is just an example. 这只是一个例子。 The essence is that you pass along objects, not primitive values, but that you assign to properties of those objects, never redefining the objects themselves. 本质是,您传递的对象不是原始值,而是传递这些对象的属性,而不用重新定义对象本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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