【问题标题】:javascript function stops after ajax callajax调用后javascript函数停止
【发布时间】:2016-03-15 16:43:59
【问题描述】:

下午好,

我有两个函数,一个在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>

现在reblink 函数在我调用 ajax 函数后立即停止。当我物理刷新页面时,它再次开始。我想要实现的是在我每次调用ajax函数之后启动这个函数。

我试图通过放置将它们合并 2 个功能:

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

但在 if(response.update==true){ } 中,它不会阻止 ajax 运行。

非常感谢任何建议。

更新 因此,在 cmets 代码中提到的更改后,现在如下所示:

                <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>

重新链接

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

以及实际产生闪烁效果的函数

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

blink('.blink');

});//]]> 

</script>

在所有这些 stil ajax 调用停止闪烁之后。

【问题讨论】:

  • 哇,eval怎么了
  • 嗯?对不起,我不是大师,我在 stackoverflow 中找到了其他人的问题代码,尝试没有工作。:)
  • eval() 需要一个字符串。这个blink() 函数返回什么?那里额外的; 是什么?这是一个明显的语法错误,会杀死整个脚本块。
  • 对不起,对这个 &lt;script type="text/javascript"&gt;//&lt;![CDATA[ $(window).load(function(){ function blink(selector){ $(selector).fadeOut('slow', function(){ $(this).fadeIn('slow', function(){ blink(this); }); }); } blink('.blink'); });//]]&gt; &lt;/script&gt; 的 Eval 调用,所以 blink 函数实际上是淡入淡出 div
  • eval 需要一个字符串。眨眼不返回一个。

标签: javascript php ajax


【解决方案1】:

这里的脚本有语法错误。

<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>

下面是正确的语法。

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

更新

我可以在您的代码中看到您没有调用reblink() 函数。你还提到你想在ajax之后调用reblink(),你应该把reblink()放在你的ajax的.done({});部分中,就像这样。

.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
            }
        });

【讨论】:

  • 好的,谢谢,我已经替换了这个脚本,但是当我调用 ajax 函数并仅在页面正常刷新时再次工作时,它仍然会停止。
  • 以上,1:不是我的问题的一部分,2。到目前为止,在 ajax 调用后没有解决问题 witj javascript stop
  • 而你只需要一个&lt;script&gt;&lt;/script&gt;的开始和结束标签
  • 在更改后更新了顶帖。 ajax调用后闪烁效果仍然停止
  • 顺便问一下blink()是什么?
猜你喜欢
  • 1970-01-01
  • 2012-01-11
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
相关资源
最近更新 更多