weixin_33744141
2020-04-15 20:20
采纳率: 0%
浏览 56

Ajax呼叫输出和超时

Ive build an ajax counter which outputs max 6 digits - e.g. "120398" and repeats the call

function updateNumbers(){
    $.ajax({
    url: totalnumbers.ajaxurl,
    data: {
        action: 'numberstotal'
    },
    success: function (response) {
        var numbers = response.toString(10).replace(/\D/g, '0').split('').map(Number);
        //console.log(numbers);
        var newHTML = [];
        for (var i = 0; i < 6; i++) {
            newHTML.push('<span>' + numbers[i] + '</span>');
        }
        $('.counter').html(newHTML.join(""));
    }
    });
    setTimeout(updateMinutes, 5000);
}

updateNumbers();

this works so far.

Question 1:

Now when there are only 4 digits (1234) I still need an output of total like this

<span>0</span>
<span>0</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>

what I get is

<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>undefined</span>
<span>undefined</span>

Question 2:

When theres a total of 200000 I want to quit the setTimeout-Function. But I dont know how to get the response out of the ajax call to check before setting the timeout.

Help would be great. It seems like I am almost there :(

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题