使用setTimeout()它可以在指定的时间启动一个功能:
setTimeout(function, 60000);
Run Code Online (Sandbox Code Playgroud)
但是,如果我想多次启动该功能怎么办?每次时间间隔过去,我都想执行该功能(每60秒,让我们说).
可能重复:
每60秒调用一次函数
每隔5秒连续调用一次javascript函数.我见过setTimeOut事件.如果我想连续不断,它会正常工作吗?
对不起我是一个菜鸟我只是想知道我是如何让这个javascript每秒运行一次?
源代码:
<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function() {
var ID = $(this).attr("id");
if(ID) {
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").prepend(html);
$("#more"+ID).remove();
}
});
} else {
$(".morebox").html('no posts to display');
}
return false;
});
});
</script>
Run Code Online (Sandbox Code Playgroud) 现在我已经困惑了一段时间了.我在jquery/ajax中得到了它的一部分:
<script type="text/javascript">
$(document).ready(function(){
jQuery.ajax({
type: "GET",
url: "sessioncheck.php",
dataType:"json",
success:function(response){
if (response) {
window.location.href = 'logout.php';
}
else {
// Process the expected results...
}
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
这很好用,但我希望这个过程每30秒重复一次.有人可以帮我吗?我已经阅读了有关setinterval的内容,但我似乎无法让它工作.
非常感谢您的所有帮助!
我想每 30 秒更新一次 WordPress 小部件中的信息。因此我想我最好使用一些 Ajax。
我在WordPress Codex上找到了这个并将其添加到我的functions.php:
add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here
function my_action_javascript() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'action': 'my_action',
'whatever': 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
alert(response);
});
});
</script> <?php
}
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
global $wpdb; // this is how you get access to …Run Code Online (Sandbox Code Playgroud) javascript ×5
ajax ×3
jquery ×2
dom-events ×1
function ×1
php ×1
setinterval ×1
timer ×1
wordpress ×1