Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON data if/else parse
    text
    copied!<p>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:</p> <pre><code>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); } </code></pre> <p>The following JSON response that gets sent to this code is:</p> <pre><code>{"current_date_time":"January 8, 2013 - 4:02 pm","dash_notify":"1"} </code></pre> <p>It understand the initial blink IF the above data gets passed. If the following gets passed:</p> <pre><code>{"current_date_time":"January 8, 2013 - 4:02 pm"} </code></pre> <p>Then it throws an error: </p> <pre><code>Uncaught ReferenceError: timer is not defined </code></pre> <p>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. </p> <p>Basically I need the else portion fixed.</p> <p>I have tried using different typeOf === undefined snippets, but it doesn't work.</p> <p>Any help is appreciated.</p> <p>Thank you!</p> <p><strong>EDIT:</strong></p> <p>This is currently working.. Timer is now defined above the statement</p> <pre><code>if(data.dash_notify == '1'){ var x = '#dash_notif_blink'; console.log("initiate_dash"); blinking($(x)); }else if (typeof timer != "undefined" &amp;&amp; timer) { clearInterval(timer); } } </code></pre> <p>This is working, but sometimes it trys to kill the timer but it doesn't actually do it. This happens every so often.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload