当没有按钮时,如何防止获取请求重新加载Java页面?

问题描述

让我以此开头,说有无休止的线程描述了涉及按钮的问题。通常,可以通过在传入的事件上调用event.preventDefault()来解决。但是,如果在用户控制范围之外的事情发生之后(例如,在一定数量的帧之后)调用了发布请求,会怎样? >

makescore (userId,gameId,time) {
      fetch('http://localhost:3000/scores',{
        method: 'POST',headers: {
          'Content-Type': 'application/json'
        },body: JSON.stringify({
          time: time,user_id: userId,maze_id: gameId
        })
      }).catch(error => console.log('ERROR'))
    };

    loadNextLevel(option) {

        console.log("Load Next");

        //  Making a score
        clearInterval(this.interval);
        this.time = document.getElementById('timer_text').innerHTML;
        this.makescore(this.userId,this.gameId,this.time);
        console.log("score made");

        if (this.GAMESTATE != GAMESTATE.GAMEOVER) {

        console.log(this.gameObjects);
        document.getElementById('timer_text').innerHTML = "Time"
        this.gameObjects = []
        this.gameIndex += 1;

        console.log(this.gameIndex,this.maxMazes);

        if (option == "new") {
          this.gameIndex = 0;
        }
      }

      if (this.gameIndex >= this.maxMazes) {
        this.gameEnd();
        return;
      }

使用此代码,我只想在调用loadNextLevel之后才调用makescore,并且仅在级别完成时才调用。但是,当前已保存分数,并且页面已刷新。如何防止这种刷新?

编辑:在下面添加了得分控制器

class scoresController < ApplicationController

    def index
        scores = score.all
        render json: scores
    end

    def show
        score = score.find(params[:id])
        render json: score
    end

    def create
        if (params[:user_id] && params[:maze_id])
            user = User.find_by(id: params[:user_id])
            maze = Maze.find_by(id: params[:maze_id])
            score = user.scores.build(time: params[:time],username: user.username)
            maze.scores << score
        end
    end

end

编辑#2-添加提取完成后显然卡住的功能

function setLoggedOutUI() {
  console.log("Log out UI set");
  document.getElementById("timer_text").style.display = "none";
  document.getElementById("logInButton").innerHTML = "Log in";
  document.getElementById("userInfo").innerHTML = "Please Log in to play";
  document.getElementById("logInField").style.display = "inline-block";
  document.getElementById("play").innerHTML = "Log in";
  document.getElementById("scores").style.display = "none";
  document.getElementById("usernameText").style.display = "inline";
  uiSwitch = false;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)