我在页面上有多个计时器,但代码似乎不适合它。
在下面的代码中,每个帖子的计时器都以onload函数启动,该函数调用jquery代码
我使用的代码我从另一个线程得到它在这里我只是修改它以使用每个跨度的onload但它不起作用
<?php
$i=0;
$timeleft="05:00:30";
while ( $loop->have_posts() ) : $loop->the_post();
?>
<span id="countdown<?php echo $i; ?>" onload="start_timer(<?php echo $i; ?>,<?php echo $timeleft; ?>)"> <?php echo $timeleft; ?> </span>
<?php $i++;endwhile;wp_reset_postdata(); ?>
<script type="text/javascript">
function start_timer(num,time){
$(document).ready(function() {
parts = time.split(':'),
hours = +parts[0],
minutes = +parts[1],
seconds = +parts[2],
span = $('#countdown'+num);
function correctNum(num) {
return (num<10)? ("0"+num):num;
}
var timer = setInterval(function(){
seconds--;
if(seconds == -1) {
seconds = 59;
minutes--;
if(minutes == -1) {
minutes = 59;
hours--;
if(hours==-1) {
alert("timer finished");
clearInterval(timer);
return;
}
}
}
span.text(correctNum(hours) + ":" + correctNum(minutes) + ":" + correctNum(seconds));
}, 1000);
});
}
</script>
答案 0 :(得分:0)
你可以这样做:
<?php
include( "Connect.php" );
ob_start(); session_start();
if (isset($_SESSION['user_login'])) {
$user = $_SESSION["user_login"];
}
else {
$username = "";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<title></title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</html>
<?php
$id = "";
if (isset($_GET['uid'])) {
$uid = mysqli_real_escape_string($mysqli,$_GET['uid']);
if (ctype_alnum($uid)) {
$get_likes = $mysqli->query("SELECT * FROM likes WHERE uid='$uid'");
if (mysqli_num_rows($get_likes)===1) {
$get = mysqli_fetch_assoc($get_likes);
$uid = $get['uid'];
$total_likes = $get['total_likes'];
$total_likes = $total_likes + 1;
$remove_likes = $total_likes - 2;
}
else
{
die("Error ...");
}
if (isset($_POST['likebutton_'])) {
$like = $mysqli->query("UPDATE likes SET total_likes='$total_likes' WHERE uid='$uid'");
$user_likes = $mysqli->query("INSERT INTO user_likes VALUES ('','$user','$uid')");
header("Location: like_but_frame.php?uid=$uid");
}
if (isset($_POST['unlikebutton_'])) {
$like = $mysqli->query("UPDATE likes SET total_likes='$remove_likes' WHERE uid='$uid'");
$remove_user = $mysqli->query("DELETE FROM user_likes WHERE uid='$uid' AND username='$user'");
header("Location: like_but_frame.php?uid=$uid");
}
}
}
//Check for previous likes
$check_for_likes = $mysqli->query("SELECT * FROM user_likes WHERE username='$user' AND uid='$uid'");
$numrows_likes = mysqli_num_rows($check_for_likes);
if ($numrows_likes >=1) {
echo '<form action="like_but_frame.php?uid=' . $uid . '" method="POST">
<input type="submit" name="unlikebutton_' . $id . '" value="Unlike">
<div style="display: inline;">
' . $total_likes . ' likes
</div>
</form>';
}
else if ($numrows_likes == 0) {
echo '
<form action="like_but_frame.php?uid=' . $uid . '" method="POST">
<input type="submit" name="likebutton_' . $id . '" value="Like">
<div style="display: inline;">
' . $total_likes . ' likes
</div>
</form>
';
}
?>
修改强>
将您的javascript更改为:
<div class="postForm">
<form action="<? echo $username; ?>" method="POST">
<textarea id="post" placeholder="160 characters limit" name="post" maxlength="160" rows="3" cols="60"></textarea>
<input type="submit" name="send" value="Post" style="background-color: #DCE5EE; float: right; border: 1px solid #666;"/>
</form>
</div>
<div class="profilePosts">
<?php
$getposts = $mysqli->query("SELECT * FROM posts WHERE user_posted_to='$username' ORDER BY id DESC LIMIT 10") or die(mysqli_error($mysqli));
while ($row = mysqli_fetch_assoc($getposts)) {
$id = $row['id'];
$body = $row['body'];
$date_added = $row['date_added'];
$added_by = $row['added_by'];
$user_posted_to = $row['user_posted_to'];
$get_user_info = $mysqli->query("SELECT * FROM users WHERE username='$added_by'");
$get_info = mysqli_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./img/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
echo "
<div style='float: left;'>
<img src='$profilepic_info' height='60'>
</div>
<div class='posted_by'>
<a href='$added_by'>$added_by</a> - $date_added - </div>
$body<br /><br /><br />
<iframe src='./like_but_frame.php?uid=$uid' frameborder='0' style='max-height: 70px; width: 20%; min-height: 10px;'></iframe>
<hr />
";
}
和你的while循环:
dr
你也可以在JSFiddle onload is only supported on the following elements
中查看它