【问题标题】:How to store the value of a global variable in an ajax call?如何在ajax调用中存储全局变量的值?
【发布时间】:2013-11-14 22:27:41
【问题描述】:

我有一个游戏想要保存高分。

这就是我现在正在做的事情:

  /* high score stuff */
  var score = 0;
  var highscore = 0;
  var scoreelement = $('#highscoredisplay');

  //showing high score
  function calchighscore(){
    highscore = $.get('highscore');
    $.ajax({
        url:'index.html',
        type: 'get',
        dataType: 'html',
        success: function(data){
            jQuery('#highscoredisplay')
        }

    //set score to wincount
    score = wincount;  
    if(score > highscore){
        highscore = score;
    }
    showscore();
  }

我的 ajax 调用不起作用,这是因为我不知道如何进行适当的调用。现在我可以随着胜利的增加而增加我的高分,但是高分在退出游戏后没有保存。

谁能帮帮我?我想我需要在我的高分显示上使用 get 但我不知道如何。我一直在搜索很多示例,但我找不到我正在寻找的东西,或者我只是不明白我在寻找什么......

提前致谢!

【问题讨论】:

    标签: jquery web-applications


    【解决方案1】:

    我会使用 PageMethod 来获得这样的分数:

    function calchighscore() {
        PageMethods.GetHighScore($.get('highscore'), OnSuccess, OnFailure);
        function OnSuccess(data) {
          score = wincount;
          if (score > data) {
              highscore = score;
          }
        function OnFailure() {
            alert('failed');
        }  
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      相关资源
      最近更新 更多