/    Sign up×
Community /Pin to ProfileBookmark

Setinterval question/problem.

Hi I am a new coder and Im unable to understand my next task.

What Ive done is use local storage to set a value in my web app.
Then my next task was to write said value on my page (This took me hours but finally got it done).
My next task is using Setinterval(or what ever the correct way to solve my problem) to subtract a number (lets use “1” for this example) Every 5 seconds from the local storage value.So basically After I click the button. Value is set and displayed. After another button is click. Every 5 seconds the localstorage is subtraced by 1. I think the code im looking for is similar to this

[CODE]setInterval(“localStorage.test” – 5 ()”,5000);[/CODE]

Im pretty sure this is wrong but I think I may have the sense right idea. Can any one help please?

“My Code”

[CODE]
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

<script type=”text/javascript”>

function setCookie(){
localStorage.test=200; //Sets the value of the localstorage
document.getElementById(‘tested’).innerHTML =localStorage.test; //sets the Id value to storage value to an id
}

</script>

</head>
<body>
<input type=”button” onclick=”setCookie()” value=”SetCookie” />
<b id=’tested’></b>
</body
</html>

[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@QuentinGauthorApr 20.2011 — Sorry for the double post but there wasnt an edit button in sight. Ive been working all night on this and Im halfway done.

Ive gotten the Interval to work and subtraction! Im so new to this im proud of myself ?. My only problem is this. I realized that the first function setcookie may not have been the right way to what I want to do. I want the text to always display what its cookie value is... I have noclue where to start.. any help please?


I Found the Edit Button. I Ended up solving a problem I had been trying to figure out the past 2 days myself. Any mod can mark topic as Resolved. And IM posting Solution for future searches

[CODE]<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

<script type="text/javascript">
function showcook(){//Updates my Id with the current Value
var s = setInterval("setCookie()",500);

}
function setCookie(){
document.getElementById('tested').innerHTML =localStorage.test; //sets the storage value to an id
}
function takeaway(){// function runs the subtract script every 5 seconds
var s = setInterval("subtract()",5000);
}
function subtract(){// subtracts from the cookie
var stornum = localStorage.test;
var taknum = 1;
var answer = stornum-taknum;
localStorage.test =answer;
}
</script>

</head>
<body>
<input type="button" onclick="setCookie()" value="Set/Show Cookie" />
<input type="button" onclick="subtract()" value="Start Subtraction" />
<input type="button" onclick="takeaway()" value="Start Intervals" />
<input type="button" onclick="showcook()" value="Start Updated Intervals" />

<b id='tested'></b>
</body>
</html>[/CODE]
×

Success!

Help @QuentinG spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...