Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Javascript]What's the difference?

Featured Replies

So i got this code and what's the difference between using the variable speed and not i mean if i remove speed it does not make any difference but if i use speed it does not show up any difference?



The one using the variable speed:

(function() {

var speed = 10,
moveBox = function() {
var el = document.getElementById("box"),
left = el.offsetLeft,
moveBy = 1;

el.style.left = left + moveBy + "px";

if (left > 399) {
clearTimeout(timer);
}

};

var timer = setInterval(moveBox, speed);


}());


The one not using the variable speed:


(function() {

var moveBox = function() {
var el = document.getElementById("box"),
left = el.offsetLeft,
moveBy = 1;

el.style.left = left + moveBy + "px";

if (left > 399) {
clearTimeout(timer);
}

};

var timer = setInterval(moveBox);


}());

setInterval requires a delay as it's second parameter, without it the code may not run properly in some browsers.

  • 2 weeks later...

You might want to try something like this:

 

function movebox(speed) {
    setInterval(function(){
        var el = document.getElementById("box"),
        left = el.offsetLeft,
        moveBy = 1;
        el.style.left = left + moveBy + "px";        
    }, speed);

    if (left > 399) {
        clearTimeout(timer);
    }

}

 

...then call the function as you need it. Haven't checked to see if this works....

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.