setTimeout回调函数不起作用

时间:2017-04-07 12:31:58

标签: javascript html

我使用onClick inside按钮调用函数。被调用的函数在一个对象内部,并且每秒使用setTimeout调用它。但回调函数不会被setTimeout调用,只适用于第一次调用。

如果我不使用object并使用一个返回startPomadoro函数的封装函数,那么它的工作。

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

void make_layout(int row_begin, int row_end, int ncol, MPI_Datatype* mpi_dtype)
{   
    int nblock = 1;
    int block_count = (row_end - row_begin + 1) * ncol;
    MPI_Aint lb, extent;
    MPI_Type_get_extent(MPI_DOUBLE, &lb, &extent);
    MPI_Aint offset = row_begin * ncol * extent;
    MPI_Datatype block_type = MPI_DOUBLE;

    MPI_Type_create_struct(nblock, &block_count, &offset, &block_type, mpi_dtype);
    MPI_Type_commit(mpi_dtype);
}

double** allocate(int nrow, int ncol)
{
    double *data = (double *)malloc(nrow*ncol*sizeof(double));
    double **array= (double **)malloc(nrow*sizeof(double*));
    for (int i=0; i<nrow; i++)
        array[i] = &(data[ncol*i]);

    return array;
}

int main()
{
    MPI_Init(NULL, NULL);

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // make 3x3 matrix.
    int nrow = 3;
    int ncol = 3;
    double** A = allocate(nrow, ncol); 

    // make mpi datatype to send rows [0, 1] excluding row 2.
    // you can send any range of rows i.e. rows [row_begin, row_end].
    int row_begin = 0;
    int row_end = 1; // inclusive.
    MPI_Datatype mpi_dtype;
    make_layout(row_begin, row_end, ncol, &mpi_dtype);

    if (rank == 0)
        MPI_Send(&(A[0][0]), 1, mpi_dtype, 1, 0, MPI_COMM_WORLD);
    else
        MPI_Recv(&(A[0][0]), 1, mpi_dtype, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);        

    MPI_Type_free(&mpi_dtype);
    MPI_Finalize();

    return 0;
}

2 个答案:

答案 0 :(得分:0)

this调用函数时,setTimeout没有出现问题。尝试这样的事情:

var pomodoroClock = {
  countPomadoro: 0,
  countBreak: 0,
  currentTimerText: '',

  startPomodoro: function() {
    var that = this;
    var pomadoroTimeInMinutes = document.getElementById('pomodoroInput').value;
    var breakInMinutes = document.getElementById('breakInput').value;
    if (this.countPomadoro < pomadoroTimeInMinutes) {
      var minutesLeftPomadoro = pomadoroTimeInMinutes - this.countPomadoro;
      that.currentTimerText = "Your have " + minutesLeftPomadoro + " Minutes Left.";
      that.countPomadoro++;
      that.displayPomadoro();
      setTimeout(function() { that.startPomodoro() }, 1000);
    } else {
      if (that.countBreak < breakInMinutes) {
        var minutesLeftBreak = that.breakInMinutes - that.countBreak;
        currentTimerText = "Your have " + minutesLeftBreak + " Minutes Left in Break.";
        that.countBreak++;
        that.displayPomadoro();
        setTimeout(function() { that.startPomodoro() }, 1000);
      } else {
        that.currentTimerText = " Break Time is UP. ";
        that.displayPomadoro();
      }
    }
  },
  displayPomadoro: function() {
    var pomodoroHtmlElement = document.createElement('p');
    var outputDiv = document.getElementById('output');
    pomodoroHtmlElement.textContent = this.currentTimerText;
    outputDiv.appendChild(pomodoroHtmlElement);
  }
}
<div class="cold-md-12 text-center">
  <button>Set Pomodoro</button>
  <input id="pomodoroInput" type="number">
  <button>Set Break</button>
  <input id="breakInput" type="number">
</div>
<div class="cold-md-12 text-center"><br>
  <button onClick="pomodoroClock.startPomodoro()">Start Pomodoro</button>
</div>
<div id="output">
</div>

PS。您还有一个拼写错误breakInMnutes而不是breakInMinutes

答案 1 :(得分:0)

我已经通过远离jquery而简单地解决了这个问题 通过使用包装每个图像在一个div内设置div上的事件处理程序 JavaScript的。下面是示例代码。

//code

handler.divs.forEach(function(div){ 
                var img = document.getElementById(div);
                img.onclick = function(){
                    var id = parseInt(img.id,10);
                    userPattern.push(id);
                    handler.effect(id);
                    console.log(" div clicked " + id + " u p " + userPattern);
                    if(userPattern.indexOf(id) !== simonGame.PATTERN.indexOf(id)){
                        console.log(" if ");
                        handleWrongInput();
                    } else if(userPattern.length === simonGame.PATTERN.length){
                        setTimeout(function(){simonGame.patternGen({result:"success"})},1200);
                    }
                }

        });
console.log(" up " + userPattern + " SGP " + simonGame.PATTERN);
相关问题