Omri
Omri

Reputation: 1656

Switch icon mode after a few seconds

Hi have the following ajax function. After click on the link the icon should be replaced from "off" to "working" icon, and at the end of the work the icon should replaced to "on".

I want that the icon will be switched back to "off" after a few seconds on "on" status. Can someone show me how to do that? I'm new in Ajax.

$.ajax({
    url: '/work', type: "GET", 
    beforeSend: function (data, jqXHR) {
        $('#status').attr("src", "/img/_working.gif");
    },
    success: function (data, textStatus, jqXHR) {
        $('#status').attr("src", "/img/_on.png");
        $("#work").html(data)
        $("#work").show()
    },
    error: function (jqXHR, textStatus, errorThrown) {
       $('#status').attr("src", "/img/_error.gif");
        alert('error')
    }
});

<img src="/img/_off.png" class="img-swap" id="status" style="width: 20px;height: 20px;"/>

Upvotes: 1

Views: 48

Answers (1)

ED-209
ED-209

Reputation: 4746

Try setTimeout:

setTimeout(function(){ $('#status').attr("src", "/img/_off.png"); }, 3000);

Where 3000 is the time delay in milliseconds

Upvotes: 3

Related Questions