在ajax成功函数中设置超时

时间:2017-09-20 19:12:01

标签: javascript jquery ajax

我很难在正确的语法中设置ajax成功函数的超时。我想要发生的是.auction_box将#A3D1A3转为5秒然后转为#FFF。这是功能:

success: function (result) {
if (result == 'ok') {
    setTimeout(function () {
    $('.auction_box').animate({
        'backgroundColor': '#A3D1A3'
    }, 500,
    }
    function (data) {
        $('.auction_box').css('background-color', '#FFF');
    });
}
}

4 个答案:

答案 0 :(得分:0)

您没有设置setTimeout函数的超时时间。需要两个参数,一个要执行的函数(您有)和一个在执行代码之前等待的数字(以毫秒为单位)。

查看此页面以获取更多信息:https://www.w3schools.com/jsref/met_win_settimeout.asp

您的代码应该是:

success: function (result) {
if (result == 'ok') {
    setTimeout(function () {
    $('.auction_box').animate({
        'backgroundColor': '#A3D1A3'
    }, 500,
    }
    function (data) {
        $('.auction_box').css('background-color', '#FFF');
    }, 5000);
}
}

答案 1 :(得分:0)

你应该在超时功能中设置颜色'#FFF'。

下面是代码:

   success: function (result) {
       if (result == 'ok') {
           $('.auction_box').animate({
            'backgroundColor': '#A3D1A3'
            }, 500);

     setTimeout(function () {
            $('.auction_box').css('background-color', '#FFF');
        } , 5000);
      }
    }

答案 2 :(得分:0)

这样的事情:

def partition_by_rating_density(X, id_col_name, n_partitions,
                                partition_col_name='partition'):
    """Segment partitions by rating density. Partitions will be more
    evenly distributed based on the number of ratings for each user.

    Parameters
    ----------
    X : PySpark DataFrame
        The ratings matrix

    id_col_name : str
        The ID column name

    n_partitions : int
        The number of partitions in the new DataFrame.

    partition_col_name : str
        The name of the partitioning column

    Returns
    -------
    with_partition_key : PySpark DataFrame
        The partitioned DataFrame
    """
    ididx = X.columns.index(id_col_name)

    def count_non_null(row):
        sm = sum(1 if v is not None else 0
                 for i, v in enumerate(row) if i != ididx)
        return row[ididx], sm

    # add the count as the last element and id as the first
    counted = X.rdd.map(count_non_null)\
               .sortBy(lambda r: r[-1], ascending=False)

    # get the count array out, zip it with the index, and then flatMap
    # it out to get the sorted index
    indexed = counted.zipWithIndex()\
                     .map(lambda ti: (ti[0][0], ti[1] % n_partitions))\
                     .toDF([id_col_name, partition_col_name])

    # join back with indexed, which now has the partition column
    counted_indexed = X.join(indexed, on=id_col_name, how='inner')

    # the columns to drop
    return counted_indexed.repartition(n_partitions, partition_col_name)\
        .drop(partition_col_name)
.auction_box{
width:100px;
height:100px;
background:blue;
}

答案 3 :(得分:0)

为什么不使用CSS转换?我觉得它简单得多。像这样:

CSS

.auction_box {
  background-color: blue;
}

.new {
  background-color: #A3D1A3;
  transition: background-color 5000ms linear;
}

.white {
  background-color: white;
}

的jQuery

$('.auction_box').addClass('new').addClass('white');

<强> DEMO