21-08-2009, 13:34
|
|
|
חבר מתאריך: 21.08.09
הודעות: 1
|
|
javascript - timeout function
2 משתנים גלובלים checkCards ו- path שמוגדרים מחוץ לפונקציה, בתוך הסקריפט. המשתנה הבוליאני לא צריך להתאפס בסוף כל ריצה אלא כל פעם להיות חיובי - שלילי לסירוגים. ולכן איפוסו לא משרת את המטרה. אחרי היפוך שני קלפים יש לבצע את הבדיקה האם הקלפים זהים או לא. משחק הזיכרון.
הפונקציה:startPausing , אכן קוראת לעצמה כי זה היה הפתרון היחיד שמצאתי כדי להפעיל את TIMEOUT. אם יש לך רעיון אחר, אני אשמח מאוד. זו הבעיה שלי, וזה אילוץ ולא הכרח. המטרה היא ששני הקלפים יישארו פתוחים למס שניות ורק אז יתבצע הצעד הבא: היפוכם בחזרה אם לא זהים ו\או פתיחתם באופן סופי, כך שלא ניתן יהיה להופכם עוד.
startPausing - יש סיבה שזו פונקציה רקורסיבית שמלבד לקרוא לעצמה כל 5 שניות אינה עושה כלום? להלן הקוד ע הערות.
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>Game to Play Studio</title>
<script type="text/javascript">
var checkCards = false;
var last_card="";
var path = "img/game/white/"
// startPausing פונקציה שמטרתה השהיית הקלפים כפי שהם פתוחים
function startPausing()
{
setTimeout("startPausing()",5000);
}
//changeImage שמקבלת קלף שהמשתמש לחץ להיפוכו. כלומר קלף הפוך והתמונה שאמורה להופיע על הקלף לאחר היפוכו.
function changeImage(img, new_src)
{//only every second time will enter the function.
img.src = new_src;
if (checkCards)
{ // create two variable that their path are equal.
var last_card_src = last_card.src.substring (last_card.src.lastIndexOf("/")+1);
new_src = new_src.substring(new_src.lastIndexOf("/")+1);
startPausing();// for Delay & timeout. does not working.
if (last_card_src == new_src)
{
img.src = path + new_src;
last_card.src = path + new_src;
}//If equal: the cards are Open for ever
else
{ //If Not equal: the cards are turned off ,till next time.
img.src = "img/game/gray.gif";
last_card.src = "img/game/gray.gif";
}
}
else
{
last_card = img; //any why last_card parameter will be img.
}
checkCards = false; // don't check next time
</script>}
השורה ששולחת אל הפונקציות היא: מתוך הבודי:
<td style="left; width:2px"><img onclick="changeImage(this,'img/game/pigi.gif')" src="img/game/gray.gif" /></td>
האם מישהו יכול לעזור לי?
|