Having read the threads on this topic, am still stuck.

For some reason, function 'raat' is only called when the page is reloaded manually. How can I get the span to refresh automatically?

<span id="sn1"></span>
<script>
function raat() {
var sesh = <?php echo json_encode($_SESSION['customer_id']); ?>;
var vx = document.getElementById("sn1");
if ( sesh > 0) {
vx.innerHTML='<li><a href="J05.php" target="iframe_m">Edit Account Info</a></li>';
} else {
vx.innerHTML='';}
}; /* end raat */

window.onload=raat();    

setInterval( raat, 3000);
</script>
有帮助吗?

解决方案

You are calling raat and assigning whatever it returns to onload

window.onload=raat();  

It needs to be

window.onload=raat;  

And hopefully you realize that <?php echo json_encode($_SESSION['customer_id']); ?>; will not keep updating, the value will be the same since it is only added when the page is rendered. If you want to get info when that is updated, you will need to make an Ajax call to the server to get the data.

其他提示

Maybe this also ( it looked confusing to be setting the interval off while the other is waiting for onload

window.onload= function() { 

   raat();    

   setInterval( raat, 3000);

};

you can fix it using addEventListener:

window.addEventListener("load", function(){
    raat();
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top