jquery - Delay Javascript button onClick using setTimeout -
i'm having problem delaying button click. i've searched through stackoverflow , i've found couple answers , it's easy enough settimeout can't work in i'm working on. here's sample of code:
ajax submits data database on submit button click
<script> $(function () { $('form').on('submit', function (e) { $.ajax({ type: 'post', url: 'post.php', data: $('form').serialize(), }); e.preventdefault(); }); }); </script>
pusher code
<script> $(function() { var pusher = new pusher('pusher') var activitychannel = pusher.subscribe('stream'); var activitymonitor = new pusheractivitystreamer(activitychannel, "#current"); var examples = new exampleactivities(activitymonitor, pusher); $("#broadcast").click(function(){ activitymonitor.sendactivity('broadcast'); }); $("#submit").click(function(){ activitymonitor.sendactivity('submit'); }); }); </script>
html buttons
<input id="submit" name="submit" type="submit" value="submit" onclick="addmem()"><br> <button id="broadcast">broadcast</button>
the onclick="addmem()" on submit button script. it's pretty busy button.
so what's happening click submit , ajax script pushes data post.php submits database. same submit click pusher code triggered pulls submitted data out of database , broadcasts that's connected.
the problem i'm having submit submits data database , triggers pusher pulls database simultaneously. 75% of time pusher code tries pull data database before submit has submitted it.
i put settimeout everywhere think of , couldn't delay pusher code. suggestions? it's ok if submit , broadcast buttons delayed. it's not ok if clicking submit delays ajax code or onclick=addmem()
sorry making complicated. final step of long project , if can working it's css, data entry , math here.
i suggest passing success callback jquery.ajax method trigger broadcasting so:
$.ajax({ type: 'post', url: 'post.php', data: $('form').serialize(), success: function () { activitymonitor && activitymonitor.sendactivity('broadcast'); } });
a solution settimeout this:
$("#broadcast").click(function(){ settimeout(function () { activitymonitor.sendactivity('broadcast') }, 2000); });
Comments
Post a Comment