连续计数器中的数字格式问题

时间:2019-04-14 12:25:09

标签: jquery counter

由于某种原因,我的连续计数器正确地显示了初始起始数字'65,000',并用千位分隔符表示,但是在此之后,它在每次计数时都无法将200增量添加到起始数字上。我想看的是


from tkinter import *
import random

class game:
    def __init__(self, root):

        def click(x,y):
            button1.place(x=x, y=y)

        def click2(x, y):
            button12.place(x=x, y=y)

        random.randint(0, 300)
        root.geometry("600x600")
        root.title("Click Me")


        button1 =  Button(root, padx=14, pady=14, bd=4, bg='white', text="test ",
                      command=lambda: click(random.randint(0, 430),
                                            random.randint(0, 430)), font=("Courier New", 16, 'bold'))

        button1.place(x=260, y=263)

        button12 = Button(root, padx=14, pady=14, bd=4, bg='white', text="test2",
                         command=lambda: click2(random.randint(0,430),
                                               random.randint(0,430)), font=("Courier New", 16, 'bold'))

        button12.place(x=100, y=100)

root = Tk()

ggame = game(root)

root.mainloop()

我怀疑我仍在错误使用我的数千个分隔符格式化功能'numberWithCommas'。我尝试从代码中的许多其他地方调用它,但是没有任何效果。我也对如何使以前的帖子适应此代码感到困惑。在这里有点深。有人可以帮忙吗? https://jsfiddle.net/cs6hL4jy/

65,000, 65,200, 65,400, etc
$(document).ready(function() {

  var StartNowTonnes = 6500000
  var timeVal = StartNowTonnes;

  $('.timer').html(timeVal);
  refreshTimer(); // Load timer function

  var timerInterval = 1000;
  var loadTimer = setInterval(refreshTimer, timerInterval);
});

//----------------------------

// FUNCTION Refresh Timer
function refreshTimer() {
  // Set counter
  setTimeout(function() {
    // Get timer value
    var myTime = parseInt($('.timer').html()) + 5;

    // If timer has not reached 0
    if (myTime > 0) {

      //$('.timer').html(myTime);
      var myTimeWithComma = numberWithCommas(myTime);
      $('.timer').html(myTimeWithComma);


    } else {
      $('.timer').html('Time up!');
    }
  }, 1);

}

//--------

function numberWithCommas(number) {
  var parts = number.toString().split(".");
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  return parts.join(".");
}
.timer {
  background: #d2d8e8;
}

1 个答案:

答案 0 :(得分:1)

您不应将当前值从页面读回到变量中。而是将值保留在内存中(即保持 state )。您当前的问题是parseInt不会按预期解释该数字。但是,如果您不依赖于解析该数字,则该问题将变得无关紧要。您是自己生成的,因此请对其进行跟踪。另外:

  • 您不需要setIntervalsetTimeout
  • 奇怪的是,您增加该数字并仍然检查该数字是否为正数,就像是倒数一样。
  • 您的代码增加5,而不是200
  • 评论可能会有所帮助,但请避免添加根本不添加任何信息的评论。像“ // FUNCTION Refresh Timer”实际上并没有告诉您任何有用的信息。

$(document).ready(function() {
  var state = {
      timerInterval: 1000,
      myTime: 6500000,
      increment: 200
  };

  refreshTimer(state);
});

function refreshTimer(state) {
    var myTimeWithComma = numberWithCommas(state.myTime);
    $('.timer').html(myTimeWithComma);
    
    // If timer has not reached 0
    if (state.myTime > 0) {
        state.myTime += state.increment;
        setTimeout(() => refreshTimer(state), state.timerInterval);
    } else {
        $('.timer').html('Time up!');
    }
}

//--------

function numberWithCommas(number) {
  var parts = number.toString().split(".");
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  return parts.join(".");
}
.timer {
  background: #d2d8e8;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="timer"></div>