正确调试Javascript问题? (很可能是客户端)

时间:2015-05-08 06:13:32

标签: javascript node.js multidimensional-array websocket socket.io

这个问题很可能是客户端问题,但我对此并不确定。这是一个拍卖网站,我每秒都使用WebSocket(node.js服务器)获取数据(价格,剩余时间)。

如果价格发生变化,我的脚本会自动触发一个事件,此次拍卖的倒计时和价格会闪烁,我的数组timers会在剩下的秒数(auction.duration)下刷新:

if ($('.price' + timers[i][0]).data('curr_text') !== $('.price' + timers[i][0]).text()) 
{
$(".flashing" + timers[i][0]).toggle("highlight").toggle("highlight");
timers[i][1] = auction.duration;        
}

所以,我在afk工作了几个小时,一些拍卖计时器变为零(显示为00:00:00),但不是所有的计时器。当我刷新页面时,这些拍卖仍在运行,因此这很可能是客户端JavaScript问题。还有一个巨大的问题,因为用户认为拍卖已经结束,刷新页面并注意到它实际上仍在运行。

我可以猜测出现这个问题,因为一些时间问题,但我不确定。此外,我不知道如何调试此问题,因为我无法在屏幕上凝视数小时等待这种情况发生。所以我很感激调试这个的建议。

我认为向您展示我的完整代码也很有帮助,也许您会看到我遗失的内容:

// prevent flashing after page loaded
$(".refresh-item").each(function() {
$this = $(this);
var id = $this.attr("data-id"); 
$('.price' + $this.attr("data-id")).data('curr_text', $('.price' + id).text());
});
// format timer
var formatSeconds = function(secs){
    var pad = function(n) {
        return (n < 10 ? "0" + n : n);
    };
    var h = Math.floor(secs / 3600);
    var m = Math.floor((secs / 3600) % 1 * 60); // Reminder of an hour of seconds x 60
    var s = Math.floor((secs / 60) % 1 * 60); // Reminder of a minute of seconds x 60
    return pad(h) +":"+ pad(m) +":"+ pad(s);
};  

var timers = [];
var socket = io.connect('http://xxx.rhcloud.com:8000/',{'forceNew':true });
socket.on('stream', function (data) {
    $.each(data.streamArray,function(index,auction){    
    duration = auction.duration;
    num_rows = data.streamArray.length;

    if (duration > 0)
    {
    var price_retail = $("#" + auction.id).attr("data-retail"); 

    var calc = auction.price.toFixed(2);
    var price_calc = calc.toString().replace(/\./g, ',');   
    $(".price" + auction.id).html(price_calc + "€");            
    $(".discount" + auction.id).html(discount + "%");

    if (timers.length < num_rows)
    timers.push([auction.id, auction.duration]);
    for(i in timers) 
    {                   
        if ($('.price' + timers[i][0]).data('curr_text') !== $('.price' + timers[i][0]).text()) 
        {
        $(".flashing" + timers[i][0]).toggle("highlight").toggle("highlight");
        timers[i][1] = auction.duration;        
        }
    }   
    $('.price' + auction.id).data('curr_text', $('.price' + auction.id).text());        
    } 
    else
    {
    $(".sold" + auction.id).html("<div class='sold'>SOLD</div>").fadeOut('fast').fadeIn('fast');            
    }
    });
});
function timerCount() {
    for(i in timers) {              
        if(timers[i][1] <= 0) {
        delete timers[i]; // if timer seconds is less than 1, delete it.
        } else {            
        timers[i][1]--; // else, decrease it by 1 second.
        duration_html = formatSeconds(timers[i][1]);        
        $(".duration" + timers[i][0]).html(duration_html);              
        }   
    }
}
setInterval(timerCount, 1000);

提及一个名为&#34; SOLD&#34;的红色标志也很重要。如果拍卖实际结束,则会出现(请参阅套接字流的结尾)。但这也没有发生。计时器刚刚停止并转到00:00:00。因此,它必须对delete timers[i]过早触发的数组执行某些操作,我猜?

其他信息: 我忘了提到这些计时器重置为大约。每次用户出价时15-16秒。所以数组timers已经存在了很长时间。这个数组每秒都会循环。

1 个答案:

答案 0 :(得分:0)

据我所知,您的计时器每1分钟被此代码删除:

function timerCount() {
    for(i in timers) {              
        if(timers[i][1] <= 0) {
            delete timers[i]; // if timer seconds is less than 1, delete it.
        } else {            
            timers[i][1]--; // else, decrease it by 1 second.
            duration_html = formatSeconds(timers[i][1]);        
            $(".duration" + timers[i][0]).html(duration_html);              
        }   
    }
}

你在哪里减少分钟和小时?

第二件事 - 您无法保证客户端和服务器上的时间是同步的,因此您应该在计时器中执行一些AJAX请求,并在一定时间间隔内调整计时器。