多维数组和时间戳

Multi-dimensional array and timestamp

本文关键字:时间戳 数组      更新时间:2023-09-26

代码如下:

var array = [];
$('.body-watch-logo').each(function () {
                array[]['evendId'] = $(this).data('event');
                array[]['timestamp'] = $(this).data('timestamp');
                $now = new Date();
                $outStr = now.getHours()+':'+now.getMinutes();
                alert($outStr);
                if(array[]['timestamp'] == $outStr) {
                        alert('Tring Tring Tring');
                        //do something here
                }
                $i++;
        });

。body-watch-logo中有几个事件,属性为eventId和startTime。我想把这些信息存储在数组中。然后比较只有小时和分钟的startTime与current-time。如果相等,则发出警报。有人知道怎么做吗?可以使用Ajax。请建议。

您增加了i,但从未初始化它或将其用作数组索引。但是您不需要这样做,因为.each()将索引作为参数传递给迭代函数。看到

http://api.jquery.com/jQuery.each/

数组的元素最好作为对象实现,而不是子数组。

var array = [];
$('.body-watch-logo').each(function (i) {
    array[i] = { event: $(this).data('event'),
                 timestamp: $(this).data('timestamp')
               };
    $now = new Date();
    $outStr = now.getHours()+':'+now.getMinutes();
    alert($outStr);
    if(array[i].timestamp == $outStr) {
            alert('Tring Tring Tring');
            //do something here
    }
});