Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Не передается значение в переменную (https://javascript.ru/forum/misc/68028-ne-peredaetsya-znachenie-v-peremennuyu.html)

Rodik131098 21.03.2017 17:12

Не передается значение в переменную
 
function Rectangle(w, h) {
this.width = w;
}
Rectangle.prototype.downShow = function() {
this.width = this.width - 1;
document.write(rect1.width)
}
var rect1 = new Rectangle(100, 200);
setInterval(rect1.downShow, 200);

Не передается значение свойства width объекта rect1 в функцию dawnShow, от этого не срабатывает setInterval, не могу понять почему.

Rodik131098 21.03.2017 17:18

Изменения в коде
 
Прошу прощения не правильно скорректировал код
function Rectangle(w) {
this.width = w;
}
Rectangle.prototype.downShow = function() {
this.width = this.width - 1;
document.write(rect1.width)
}
var rect1 = new Rectangle(100);
setInterval(rect1.downShow, 200);

Rodik131098 21.03.2017 17:18

Изменения в коде
 
Прошу прощения не правильно скорректировал код
function Rectangle(w) {
this.width = w;
}
Rectangle.prototype.downShow = function() {
this.width = this.width - 1;
document.write(rect1.width)
}
var rect1 = new Rectangle(100);
setInterval(rect1.downShow, 200);

Dilettante_Pro 21.03.2017 17:56

function Rectangle(w) {
   this.width = w;
   var that = this;
   this.downShow = function() {
      that.width = that.width - 1;
      console.log(that.width, rect1.width);
   }
}
var rect1 = new Rectangle(100);
setInterval(rect1.downShow, 200);

ksa 22.03.2017 09:17

Цитата:

Сообщение от Rodik131098 (Сообщение 448064)
Прошу прощения не правильно скорректировал код
function Rectangle(w) {
this.width = w;
}
Rectangle.prototype.downShow = function() {
this.width = this.width - 1;
document.write(rect1.width)
}
var rect1 = new Rectangle(100);
setInterval(rect1.downShow, 200);

У меня он работает...
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=windows-1251' />
<!--
<script src='http://code.jquery.com/jquery-latest.js'></script>
<script src="https://code.angularjs.org/1.3.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.9/angular-route.js"></script>
-->
<style type='text/css'>
</style>
<script type='text/javascript'>
function Rectangle(w) {
	this.width = w;
}
Rectangle.prototype.downShow = function() {
	this.width = this.width - 1;
	document.write(rect1.width)
}
var rect1 = new Rectangle(100);
setInterval(rect1.downShow, 200);
</script>
</head>
<body>
</body>
</html>

Dilettante_Pro 22.03.2017 11:30

ksa,
Где же он работает, когда все время 100, а должно уменьшаться

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function Rectangle(w) {
   this.width = w;
   var that = this;
   this.downShow = function() {
      that.width = that.width - 1;
      document.write(rect1.width + " ");
   }
}
var rect1 = new Rectangle(100);
setInterval(rect1.downShow, 200);
</script>
</head>
<body>
</body>
</html>

ksa 22.03.2017 11:43

Цитата:

Сообщение от Dilettante_Pro
все время 100, а должно уменьшаться

Вона че! :blink:
ТС писал, что
Цитата:

Сообщение от Rodik131098
не срабатывает setInterval

я думал в этом дело... :)


Часовой пояс GMT +3, время: 22:02.