1

I have a dynamically generated post, where i use ajax to find, the no of comments and likes in it. so, i gave a onmouseovr selector and called the js function. but i want it to automatically be called once every 2 seconds. how should i do it?

echo "</div>";

//the div has to be updated every 2-3 seconds.
//but i dont know how. so, i gave a mouseoverfunction.

echo '<div onmouseover="yopahshareloader(';
echo "'" .$id. "',";
echo "'" .$postype. "'";
echo ');" id="'.$id.'_action" style="height:20px;width:100%;margin-bottom:5px;overflow:hidden;">' ;
echo '</div>';
3
  • setTimeout(function(){ function_name(); }, 2000); Commented Nov 16, 2014 at 6:04
  • @fees setTimeout calls the function once, not repeatedly Commented Nov 16, 2014 at 6:07
  • but, what if there are arguments for the function? do i still do the same? Commented Nov 16, 2014 at 6:17

1 Answer 1

2

Use setInterval

<script type="text/javascript">
    window.setInterval(function() {
        yopahshareloader('<?php echo $id; ?>', '<?php echo $postype; ?>');
    }, 2000);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

what you gave would be correct if only a single div has to be updated. but there are many divs, its like a social networking site. and number posts could be anything. so, i have to call the function for each post individually. :\
I see. Then just add a php foreach look around the function name above then, echoing out the different $ids/$postypes. Without seeing more of your code, it will be hard to give a complete example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.