本文介绍了JSON数据if/else解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些长时间轮询(ajax),并且正在循环下面的代码部分..上面和下面正在执行代码.此代码是内部消息传递系统的一部分.消息到达时,页面的特定部分将闪烁.如果用户检查消息,它将从JSON响应中删除dash_notify,需要关闭闪烁.见下文:

I am doing some long polling (ajax) and I am looping the following portion of code.. There is code being executed above and below. This code is part of an internal messaging system. A certain portion of the page wil blink when a message arrives. If the user checks the message, it will remove the dash_notify from the JSON response, which needs to turn off the blinking. See below:

if (data.dash_notify == '1') {
    var x = '#dash_notif_blink';

    function blinking(x) {
        timer = setInterval(blink, 10);

        function blink() {
            x.fadeOut(400, function () {
                x.fadeIn(400);
            });
        }
    }

    console.log("initiate_dash");
    blinking($(x));
} else if (!data.dash_notify) {
    console.log("good");

    clearInterval(timer);
}

以下发送到此代码的JSON响应是:

The following JSON response that gets sent to this code is:

{"current_date_time":"January 8, 2013 - 4:02 pm","dash_notify":"1"}

如果以上数据通过,它将理解初始闪烁.如果通过以下条件:

It understand the initial blink IF the above data gets passed. If the following gets passed:

{"current_date_time":"January 8, 2013 - 4:02 pm"}

然后它会引发错误:

Uncaught ReferenceError: timer is not defined

我不知道如何解决其他"部分的正常工作.如果在发送完整的dash_notify:1响应时启动了代码,则它可以正常工作.该按钮将闪烁,然后,如果用户检查该消息,它将不再发送dash_notify:1,并且该按钮停止闪烁.但是,如果未设置dash_notify:1时启动了代码,则它将不知道如何使用ClearInterval.

I cannot figure out how to fix the "else" portion working properly. If the code is initiated when the full dash_notify:1 response is sent, it works perfect. The button will blink, then if the user checks the message, it will no longer send dash_notify:1 and the button stops blinking. But if the code is initiated when dash_notify:1 is NOT set, it doesn't know what to do with the ClearInterval.

基本上我需要修复else部分.

Basically I need the else portion fixed.

我尝试使用不同的typeOf ===未定义的代码段,但是它不起作用.

I have tried using different typeOf === undefined snippets, but it doesn't work.

感谢您的帮助.

谢谢!

当前正在工作.现在在语句上方定义了计时器

This is currently working.. Timer is now defined above the statement

if(data.dash_notify == '1'){

                            var x = '#dash_notif_blink';

                        console.log("initiate_dash");
                        blinking($(x));

                        }else if (typeof timer != "undefined" && timer) {
                            clearInterval(timer);
    }
                    }

这是可行的,但有时它会尝试终止计时器,但实际上并没有执行.这种情况经常发生.

This is working, but sometimes it trys to kill the timer but it doesn't actually do it. This happens every so often.

推荐答案

现在效果很好.* 谢谢所有帮助过的人! *

if(data.dash_notify ==='1'& t === null){

if(data.dash_notify === '1' && t === null ){

                    var x = '#dash_notif_blink';

                    function blinking(x) {
                        var timer = setInterval(blink, 10); //note the var keyword for best practice

                        function blink() {
                            x.fadeOut(400, function () {
                                x.fadeIn(400);
                            });
                        }
                        return timer;
                    }

                    console.log('initiate_dash_alert');

                    // Data is passed. Parse to see if dash alert should be called. Secondary protection for
                    // multiple timer creation.
                    if(t){return;}else{t = blinking($(x));}




                }else if (!data.dash_notify){
                    clearInterval(t);
                    console.log('clear_dash_alert');
                    t = null;
                }else{
                    console.log(t);
                    console.log('no_push_events');

                }

这篇关于JSON数据if/else解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:17