jquery: Flash messages

前端 未结 8 1702
再見小時候
再見小時候 2021-02-12 21:05

With jQuery, how can I display a flash message at the top of the page in an easy way? Is there something built-in, or a plugin, or is it easy enough to do it yourself?

W

8条回答
  •  时光取名叫无心
    2021-02-12 21:50

    I just added this to my page, displays a loading indicator for each ajax call (no matter where it is on the page)

    /* Ajax methods */
    /* show the message that data is loading on every ajax call */
    var loadingMessage = 'Please wait loading data for page...';
    $(function()
    {
        $("#AjaxStatus")
        .bind("ajaxSend", function()
        {
            $(this).text(loadingMessage);
            $(this).show();
        })
        .bind("ajaxComplete", function()
        {
            $(this).hide();
        });
    });
    

    And the status:

    
    

    OK, based on your most recent comment, how about this option default message, or complex:

    ShowStatus(true, 'Save Failed with unknown Error', 4000);
    
    /* show message for interval */
    var saveMessageText = 'Saving...';
    function ShowStatus(saveMessage, message, timeInMilliseconds)
    {
        var errorMessage = $("#Errorstatus");
        if (saveMessage)
        {
            errorMessage.show();
            var myInterval = window.setInterval(function()
            {
                message = message + '...';
                errorMessage.text(message);
                errorMessage.show();
            }, 1000);
            window.setTimeout(function()
            {
                clearInterval(myInterval);
                errorMessage.hide();
            }, timeInMilliseconds);
        }
        else
        {
            errorMessage.text(message);
            errorMessage.show();
            window.setTimeout('$("#Errorstatus").hide()', timeInMilliseconds);
        };
    };
    

    Snip from jquery ajax:

       success: function(msg)
        {
            ShowStatus(true, 'Hey it went well', 4000);
            Process(msg);
        },
    
      failure: function(msg)
        {
            ShowStatus(true, 'Save Failed with unknown Error', 4000);
        }
    

提交回复
热议问题