Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,513 members, 7,819,857 topics. Date: Tuesday, 07 May 2024 at 03:37 AM

How Can I Make Countdown Value Available Outside Its Scope In JS - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Can I Make Countdown Value Available Outside Its Scope In JS (376 Views)

Just Made A Game In Js / Web Developers / Freelaancers: How Are You Getting Paid From Outside Nigeria? / Tutorials On Having Deep Understanding About Loops In JS And How To Iterate (2) (3) (4)

(1) (Reply) (Go Down)

How Can I Make Countdown Value Available Outside Its Scope In JS by HydraFeeds(m): 9:34pm On Oct 21, 2023
I want to implement a countdown function outside is scope but I could not access its value in the global scope,I’ve tried a few logic but couldn’t decode it so I thought I should throw it here .

Here’s the code :



let countdownInterval; // Declare the variable in the global scope
let countdown duration = null;

if (ABC) {
// Start a five-minute countdown (300,000 milliseconds)
countdownduration = 300000; // Set the value when the countdown begins

countdownInterval = setInterval(function() {
countdownduration -= 1000; // Decrease by 1 second

if (countdownduration <= 0) {

clearInterval(countdownInterval);

console.log("Five-minute countdown is over!"wink;
} else {
Console.log(“xyz “);
}
}, 1000); // Run the countdown every second (1000 milliseconds)
}

// I want to use it outside its scope like this
if (countdownduration < 280000) {
// Countdown is less than 280,000 milliseconds
Logic xyz
}

How can I make the countdown duration value be available in the global scope pls ?
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by LikeAking: 9:59pm On Oct 21, 2023
Use var ..

var item;

If na React, u use state..

1 Like

Re: How Can I Make Countdown Value Available Outside Its Scope In JS by BlackhatMentor: 8:30am On Oct 22, 2023
Post the code somewhere where it's readable to get better replies.
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Tobedated(m): 10:28am On Oct 22, 2023
Post a screenshot of your code
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Unbiased1: 7:42pm On Oct 23, 2023
HydraFeeds:
I want to implement a countdown function outside is scope but I could not access its value in the global scope,I’ve tried a few logic but couldn’t decode it so I thought I should throw it here .

Here’s the code :



let countdownInterval; // Declare the variable in the global scope
let countdown duration = null;

if (ABC) {
// Start a five-minute countdown (300,000 milliseconds)
countdownduration = 300000; // Set the value when the countdown begins

countdownInterval = setInterval(function() {
countdownduration -= 1000; // Decrease by 1 second

if (countdownduration <= 0) {

clearInterval(countdownInterval);

console.log("Five-minute countdown is over!"wink;
} else {
Console.log(“xyz “);
}
}, 1000); // Run the countdown every second (1000 milliseconds)
}

// I want to use it outside its scope like this
if (countdownduration < 280000) {
// Countdown is less than 280,000 milliseconds
Logic xyz
}

How can I make the countdown duration value be available in the global scope pls ?

Please post a screenshot of the code, writing it on Nairaland can make it very confusing and difficult to read.
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Unbiased1: 7:44pm On Oct 23, 2023
LikeAking:
Use var ..

var item;

If na React, u use state..

var in 2023 when "let" is available?
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by LikeAking: 8:08pm On Oct 23, 2023
Unbiased1:


var in 2023 when "let" is available?

Bar and let are two diff things na..


Newbie be calm
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Unbiased1: 8:21pm On Oct 23, 2023
LikeAking:


Bar and let are two diff things na..


Newbie be calm

Ok oo. Since you still use var when declaring variables in 2023, I'm sure you also know nothing about arrow functions and spread operators. "const" may also sound strange to you.
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by LikeAking: 8:26pm On Oct 23, 2023
Unbiased1:


Ok oo. Since you still use var when declaring variables in 2023, I'm sure you also know nothing about arrow functions and spread operators. "const" may also sound strange to you.

Oga just get up..

We should be bragging about programming knowledge, instead we should brag about how many apps we have successfully lunched into the market..


What’s wrong with var?

Wetin be arrow funcs?

Wetin be spreed operator?

These are baby things..



What’s wrong with var?

Var has it own work in JS, go learn..
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by LikeAking: 8:38pm On Oct 23, 2023
Next JS devs still use var in 2023...

Be humble

Sit down..

I no dey drag or brag about knowledge of programing... The goal is to have my own products in the market...

This is my last reply to you..

You will find this code in the next/dist folder -> dev

Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Unbiased1: 8:45pm On Oct 23, 2023
LikeAking:
Next JS devs still use var in 2023...

Be humble

Sit down..

I no dey drag or brag about knowledge of programing... The goal is to have my own products in the market...


Lol. This one dey open node modules to show me var cheesy. Var is not even the solution to the op's question.
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by silento(m): 11:00am On Oct 24, 2023
Unbiased1:


Ok oo. Since you still use var when declaring variables in 2023, I'm sure you also know nothing about arrow functions and spread operators. "const" may also sound strange to you.



Bros be calming down var and let are not the same ,people are still using var and will continue using it till the kingdom come , yes it might not be the solution here but var is still a thing
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by LikeAking: 4:18pm On Oct 24, 2023
Unbiased1:


Lol. This one dey open node modules to show me var cheesy. Var is not even the solution to the op's question.

You know nothing..

U no humble.


Then give him the solution to the problem..

You are one of those new programmers enchanted by basic things..

I Dey use React… Arrow function na norms for dia..

You just disgrace ya self… You wia trying to rubbish me, abi?

Still waiting for ya second project…

Na calculator things be ur fancy, na calculator Zuck take blow?
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Deicide: 6:17pm On Oct 25, 2023
Well whatever you do don't use var

1 Like

Re: How Can I Make Countdown Value Available Outside Its Scope In JS by BlackhatMentor: 8:10pm On Oct 25, 2023
Deicide:
Well whatever you do don't use var

const and let are the new kids on the block.

Doesn't mean var is obsolete grin

It's like saying one shouldn't use

function foo(){}

Because

const foo = ()=>{}

Exist grin
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Deicide: 8:28pm On Oct 25, 2023
It's not the same thing. People might have a solid reason for using functions foo(). Like, it supports hoisting. It also holds value of this keyword accurately, unlike arrow functions. Var is discouraged against because of security risk and unpredictability it introduces to codes.
BlackhatMentor:


const and let are the new kids on the block.

Doesn't mean var is obsolete grin

It's like saying one shouldn't use

function foo(){}

Because

const foo = ()=>{}

Exist grin
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by BlackhatMentor: 8:34pm On Oct 25, 2023
Deicide:
It's not the same thing. People might have a solid reason for using functions foo(). Like, it supports hoisting. It also holds value of this keyword accurately, unlike arrow functions. Var is discouraged against because of security risk and unpredictability it introduces to codes.

Many ways to do same thing with JavaScript it's annoying.


I also believe they should once and for all remove == comparison and let us use ===instead

== Is pointless
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Deicide: 9:16pm On Oct 25, 2023
== is not pointless, as you could use it to compare primitive values in JavaScript. Not everyone needs strict equality, but i get your point.
BlackhatMentor:


Many ways to do same thing with JavaScript it's annoying.


I also believe they should once and for all remove == comparison and let us use ===instead

== Is pointless
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by BlackhatMentor: 9:23pm On Oct 25, 2023
Deicide:
== is not pointless, as you could use it to compare primitive values in JavaScript. Not everyone needs strict equality, but i get your point.

Then var is not pointless

Since not everyone needs strict variable declaration cheesy
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by Deicide: 9:28pm On Oct 25, 2023
Well true if they want buggy code
BlackhatMentor:


Then var is not pointless

Since not everyone needs strict variable declaration cheesy
Re: How Can I Make Countdown Value Available Outside Its Scope In JS by BlackhatMentor: 9:33pm On Oct 25, 2023
Deicide:
Well true if they want buggy code
.
You probably shouldn't be using any npm package since they consist of infinite var declr

(1) (Reply)

. / Developertelk.website / Help For CSS

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.