简体   繁体   English

JavaScript 淡出功能在第二次调用时不起作用

[英]JavaScript fadeOut function doesn't work on second call

I have the below function which is working properly for the first time it is called, but on second function call only alert is working .我有下面的函数,它第一次被调用时正常工作,但在第二次函数调用时,只有 alert 正在工作。 Can any one pl help me on why the next three lines dosen't work on second, third and so on calls任何人都可以帮助我解释为什么接下来的三行在第二个,第三个等电话中不起作用

function loginData(){ 
        var form = $("#signInForm");
    $.ajax({
            type: 'POST',
            url: '/signin/signin',
            data: form.serialize(),
            success: function(data){           
        if(data == "Success")
        { 
            location.replace('<?php echo BASE_HREF ?>/profile');    
        }
        else
        { alert("hello");//this alert is coming but next three lines don't get executed second and next all time
          $('#msg').text(data);
          $('#msg').css('color', 'red');    
          $('#msg').fadeOut(2000);
        }               
        //$('#msg').text('<?php echo $errormsg[jserr_msg]; ?>');    
        }                                         
         });                    
    }

The first call hides the #msg element using fadeOut .第一次调用使用fadeOut隐藏#msg元素。 If you want it to show again on subsequent calls, show it:如果您希望它在后续调用中再次显示,请显示:

$('#msg').text(data);
$('#msg').css('color', 'red');    
$('#msg').show();
$('#msg').fadeOut(2000);

Remove #msg contents and redisplay it when the fadeout is complete删除#msg内容并在淡出完成时重新显示它

$('#msg').fadeOut(2000, function(){
   $('#msg').empty();
   $('#msg').show();
});

Here an example showing how you can fix your problem, I've set Intervals to show the difference between both functions.这是一个显示如何解决问题的示例,我设置了 Intervals 以显示两个函数之间的差异。

The call on the first one is being called, however you're not showing the msg again before fading it out again.正在调用第一个调用,但是在再次淡出之前您没有再次显示msg So it'll look like it's not actually working, then with the second one it displays the message again by using .show() before using fadeOut()所以看起来它实际上没有工作,然后在第二个使用fadeOut()之前使用.show()再次显示消息

 setInterval(function(){ notWorkingAgain() }, 2000); setInterval(function(){ WorkingAgain() }, 2000); function notWorkingAgain() { $('#msg').text("ERROR MESSAGE OR SUCCESS"); $('#msg').css('color', 'red'); $('#msg').fadeOut(4000); } function WorkingAgain() { $('#msg2').text("ERROR2 MESSAGE OR SUCCESS"); $('#msg2').css('color', 'red'); $('#msg2').show(); $('#msg2').fadeOut(4000); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="msg"> ERROR MESSAGE OR SUCCESS </div> <div id="msg2"> ERROR2 MESSAGE OR SUCCESS </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM