Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've attempted, and failed, to reproduce your problem on JS Bin:<br> <a href="http://jsbin.com/ereha" rel="nofollow noreferrer">http://jsbin.com/ereha</a> (editable via <a href="http://jsbin.com/ereha/edit" rel="nofollow noreferrer">http://jsbin.com/ereha/edit</a>)</p> <p>The code you've shown us so far seems perfectly fine, so the problem must be caused by some other part of your code or system. We're all just shooting in the dark if we don't know what the issue is.</p> <p>Please try and reproduce the problem on <a href="http://jsbin.com" rel="nofollow noreferrer">http://jsbin.com</a> and we can help you from there.</p> <h2>Complete Source Code</h2> <h3>index.html</h3> <pre><code>&lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;http://stackoverflow.com/questions/1831384/javascript-variable-value-gets-lost-between-functions&lt;/title&gt; &lt;script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $.ajaxSetup({url: 'test.json'}); function updateSliderContent(json) { //&lt;- json defined here is correct var screen_order = json.screen_order.split('_'); jQuery.each(screen_order, function(i, item) { var screen_id = item; //at this point it is not, thus the function does not execute whatever is in the if blocks if (json[screen_id].action == 'add') { console.log(screen_id, 'action add'); } else if (json[screen_id].action == 'remove') { console.log(screen_id, 'action remove'); }; }); } function updateSlider() { var ajaxData = {}; jQuery.ajax({ data: ajaxData, dataType: 'json', success: function (json) { updateSliderContent(json); } }); // theDate = Math.round(new Date().getTime()/1000.0); //UNIX Timestamp sliderTimer = setTimeout('updateSlider();',15000); }; $(updateSlider); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h3>test.json</h3> <pre><code>{ 'screen_order': 'foo_bar_baz', 'foo': { 'action': 'add' }, 'bar': { 'action': 'add' }, 'baz': { 'action': 'remove' } } </code></pre>
 

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