简体   繁体   English

javascript函数在ajax调用后停止

[英]javascript function stops after ajax call

Good Afternoon, 下午好,

I have got two functions, one in ajax another one in js as below: 我有两个函数,一个在ajax中,另一个在js中,如下所示:

<script>
    /* AJAX request to checker */
    function check(){
        $.ajax({
            type: 'POST',
            url: 'checker.php',
            dataType: 'json',
            data: {
                counter:$('#message-list1').data('counter')
            }
        }).done(function( response ) {
            /* update counter */
            $('#message-list1').data('counter',response.current);
            /* check if with response we got a new update */
            if(response.update==true){
                $('#message-list1').html(response.news);
                var audio = new Audio('ding.mp3');
            audio.play();
            }
        });

    }

    //Every 20 sec check if there is new update
    setInterval(check,2000);
</script>
<script>
    function reblink() {
        $.getScript($('script:first',data).attr('src'), function(){
           eval(blink('.blink'););
        });
</script>

now reblink function stops right after I call ajax function. 现在reblink函数在我调用ajax函数后reblink停止。 Again it does starts when I physically refresh page. 当我物理刷新页面时,它确实再次启动。 What I want to achieve is to start this function after my each call of the ajax function. 我要实现的是在每次调用ajax函数之后启动该函数。

I was trying to merging them 2 functions by placing: 我试图通过放置合并两个功能:

$.getScript($('script:first',data).attr('src'), function(){
    eval(blink('.blink'););

In if(response.update==true){ } however it does than stop ajax from functioning. 但是在if(response.update==true){ }中,它确实停止了ajax的运行。

Any suggestions much appreciated. 任何建议,不胜感激。

UPDATE So after changes mentioned in comments code is now looking like below: 更新因此,在注释代码中提到的更改之后,现在看起来如下所示:

                <script>
    /* AJAX request to checker */
    function check4(){
        $.ajax({
            type: 'POST',
            url: 'checker.php',
            dataType: 'json',
            data: {
                counter:$('#last-orders').data('counter')
            }
        }).done(function( response ) {   /* update counter */
        $('#last-orders').data('counter',response.current);
        /* check if with response we got a new update */
        if(response.update==true){
            $('#last-orders').html(response.news4);
            var audio = new Audio('ding.mp3');
        audio.play();
        reblink() // call reblink here to execute
        }
    });
    }
    //Every 20 sec check if there is new update
    setInterval(check4,2000);
</script>

reblink 重新闪烁

<script>
function reblink() {
    $.getScript($('script:first',data).attr('src'), function(){
       eval(blink('.blink'));
    });
    }
</script>

and function that actually do the blink effect 和功能实际上起到了眨眼的作用

<script type="text/javascript">//<![CDATA[
$(window).load(function(){
function blink(selector){
$(selector).fadeOut('slow', function(){
    $(this).fadeIn('slow', function(){
        blink(this);
    });
});
}

blink('.blink');

});//]]> 

</script>

And after all of those stil ajax call do stop blinking. 在所有这些Stil Ajax调用之后,请停止闪烁。

This script here has an error in its syntax. 此脚本的语法有误。

<script>
    function reblink() {
        $.getScript($('script:first',data).attr('src'), function(){
           eval(blink('.blink');); // the (;) is the error  
        });
    //} this closing bracket for the function here is missing
</script>

And here is the correct syntax below. 这是下面的正确语法。

<script>
    function reblink() {
        $.getScript($('script:first',data).attr('src'), function(){
           eval(blink('.blink'));
        });
    }
</script>

UPDATE 更新

I can see there in your code that you are not calling reblink() function. 我可以在您的代码中看到您没有调用reblink()函数。 And you mentioned also there that you want to call reblink() after ajax , you should put reblink() inside in your .done({}); 您还提到了要在ajax之后调用reblink() ,应将reblink()放在.done({}); portion of your ajax like this. 像这样的部分ajax。

.done(function( response ) {   /* update counter */
            $('#message-list1').data('counter',response.current);
            /* check if with response we got a new update */
            if(response.update==true){
                $('#message-list1').html(response.news);
                var audio = new Audio('ding.mp3');
            audio.play();
            reblink() // call reblink here to execute
            }
        });

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

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