我需要计算记忆游戏的总持续时间。目前我可以计算点击次数,但我需要弄清楚以秒为单位打开所有图像需要多长时间。如何将其发送到数据库?我尝试了一些代码,但没有运气。这是我第一次使用JavaScript,所以如果答案很明显,请原谅我。谢谢你的任何建议。
更新:@ Pieter21不幸的是它没有用。我收到一个错误:String未被识别为有效的DateTime。目前"持续时间"是我的localDB中的时间戳。 Javascript:
<script>
var BoxOpened = "";
var ImageOpened = "";
var startTime = new Date().getTime();
var Counter = 0;
var ImageFound = 0;
var Source = "#memorybox";
var ImageSource = [
"Images/animals/cow.jpg",
"Images/animals/cow2.jpg",
"Images/animals/sheep.jpg",
"Images/food/lemon.jpg",
"Images/animals/elephant.jpg",
"Images/animals/giraffe.jpg",
"Images/food/banana.jpg",
"Images/animals/dog.jpg",
"Images/animals/horse.jpg",
"Images/animals/lion.jpg"
];
function RandomFunction(MaxValue, MinValue) {
return Math.round(Math.random() * (MaxValue - MinValue) + MinValue);
}
function ShuffleImages() {
var ImageAll = $(Source).children();
var ImageThis = $(Source + " div:first-child");
var ImageArr = new Array();
for (var i = 0; i < ImageAll.length; i++) {
ImageArr[i] = $("#" + ImageThis.attr("id") + " img").attr("src");
ImageThis = ImageThis.next();
}
ImageThis = $(Source + " div:first-child");
for (var z = 0; z < ImgageAll.length; z++) {
var RandomNumber = RandomFunction(0, ImgArr.length - 1);
$("#" + ImageThis.attr("id") + " img").attr("src", ImageArr[RandomNumber]);
ImageArr.splice(RandomNumber, 1);
ImageThis = ImageThis.next();
}
}
function ResetGame() {
ShuffleImages();
$(Source + " div img").hide();
$(Source + " div").css("visibility", "visible");
Counter = 0;
$("#success").remove();
$("#counter").html("" + Counter);
BoxOpened = "";
ImageOpened = "";
ImageFound = 0;
return false;
}
function OpenCard() {
var id = $(this).attr("id");
if ($("#" + id + " img").is(":hidden")) {
$(Source + " div").unbind("click", OpenCard);
$("#" + id + " img").slideDown('fast');
if (ImageOpened == "") {
BoxOpened = id;
ImageOpened = $("#" + id + " img").attr("src");
setTimeout(function () {
$(Source + " div").bind("click", OpenCard)
}, 300);
} else {
CurrentOpened = $("#" + id + " img").attr("src");
if (ImageOpened != CurrentOpened) {
setTimeout(function () {
$("#" + id + " img").slideUp('fast');
$("#" + BoxOpened + " img").slideUp('fast');
BoxOpened = "";
ImageOpened = "";
}, 400);
} else {
$("#" + id + " img").parent().css("visibility", "hidden");
$("#" + BoxOpened + " img").parent().css("visibility", "hidden");
ImageFound++;
BoxOpened = "";
ImageOpened = "";
}
setTimeout(function () {
$(Source + " div").bind("click", OpenCard)
}, 400);
}
Counter++;
$("#<%=HiddenField1.ClientID%>").val(Counter);
$("#counter").html("" + Counter);
if (ImageFound == ImageSource.length) {
$("#counter").prepend('<span id="success" runat="server">You Found All Pictues With </span>');
setTimeout(function () {
var endTime = new Date().getTime();
console.log("duration = " + (endTime - startTime));
}, 1500);
//$("#HiddenField2").html("" + (endTime - startTime));
//alert(endTime - startTime);
$("#<%=HiddenField2.ClientID%>").val(endTime - startTime);
}
}
}
$(function () {
for (var y = 1; y < 3 ; y++) {
$.each(ImageSource, function (i, val) {
$(Source).append("<div id=card" + y + i + "><img src=" + val + " />");
});
}
$(Source + " div").click(OpenCard);
ShuffleImages();
});
HTML:
<span id="Span1" class="button" runat="server">
<span id="counter" >0 </span>
Clicks </span>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
<span class="button">
<a onclick="ResetGame();">Reset</a>
</span>
<asp:Button ID="Save" runat="server" Text="Save" OnClick="Button1_Click" CssClass="button" />
C#:
SqlConnection connection = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int value = int.Parse(HiddenField1.Value);
DateTime time = Convert.ToDateTime(HiddenField2.Value);
connection = DatabaseConnectionSetup();
SqlCommand submitCommand;
try
{
submitCommand = new SqlCommand("ResultInsert", connection);
submitCommand.CommandType = CommandType.StoredProcedure;
submitCommand.Parameters.AddWithValue("@Duration", HiddenField2.Value);
submitCommand.Parameters.AddWithValue("@UserScore", HiddenField1.Value);
connection.Open();
submitCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
connection.Close();
}
}
private SqlConnection DatabaseConnectionSetup()
{
try
{
ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["MonkeyClubConnectionString"];
if (connectionString != null)
{
connection = new SqlConnection(connectionString.ConnectionString);
}
}
catch (Exception ex)
{
}
return connection;
}
答案 0 :(得分:0)
您只在客户端进行游戏长度计算。
我认为你已经表明了将其存储在“隐藏字段2”中的一些意图,但是你没有遵循。取消注释ASP.NET中的字段,在javascript中用Javascipt填充它,并将其复制到C#代码隐藏中。
您还必须确保您在字段中存储的内容可以被识别为SQL中的日期时间。